diff --git a/msi.gama.core/src/msi/gama/util/matrix/GamaFloatMatrix.java b/msi.gama.core/src/msi/gama/util/matrix/GamaFloatMatrix.java index 6d2e3a125c..59eee2e3c9 100644 --- a/msi.gama.core/src/msi/gama/util/matrix/GamaFloatMatrix.java +++ b/msi.gama.core/src/msi/gama/util/matrix/GamaFloatMatrix.java @@ -251,7 +251,7 @@ public Double get(final IScope scope, final int col, final int row) { @Override public void set(final IScope scope, final int col, final int row, final Object obj) throws GamaRuntimeException { - if (((col < numCols) && (col >= 0) && (row < numRows) && (row >= 0))) { + if (col < numCols && col >= 0 && row < numRows && row >= 0) { final double val = Cast.asFloat(scope, obj); getMatrix()[row * numCols + col] = val; } @@ -459,7 +459,7 @@ public GamaFloatMatrix minus(final Integer val) throws GamaRuntimeException { @Override public Double getNthElement(final Integer index) { - if ((index == null) || (index > getMatrix().length)) return 0d; + if (index == null || index > getMatrix().length) return 0d; return getMatrix()[index]; } @@ -467,11 +467,11 @@ public Double getNthElement(final Integer index) { protected void setNthElement(final IScope scope, final int index, final Object value) { getMatrix()[index] = Cast.asFloat(scope, value); } - - @Override - public String serialize(final boolean includingBuiltIn) { - return "matrix(" + getRowsList(null).serialize(includingBuiltIn) + ")"; - } + // + // @Override + // public String serialize(final boolean includingBuiltIn) { + // return "matrix(" + getRowsList(null).serialize(includingBuiltIn) + ")"; + // } @Override public IContainerType getGamlType() { diff --git a/msi.gama.core/src/msi/gama/util/matrix/GamaIntMatrix.java b/msi.gama.core/src/msi/gama/util/matrix/GamaIntMatrix.java index eeb5732dbb..19e607cfb1 100644 --- a/msi.gama.core/src/msi/gama/util/matrix/GamaIntMatrix.java +++ b/msi.gama.core/src/msi/gama/util/matrix/GamaIntMatrix.java @@ -502,7 +502,7 @@ public GamaIntMatrix minus(final Integer val) throws GamaRuntimeException { @Override public Integer getNthElement(final Integer index) { - if ((index == null) || (index > getMatrix().length)) return 0; + if (index == null || index > getMatrix().length) return 0; return getMatrix()[index]; } @@ -510,11 +510,11 @@ public Integer getNthElement(final Integer index) { protected void setNthElement(final IScope scope, final int index, final Object value) { getMatrix()[index] = Cast.asInt(scope, value); } - - @Override - public String serialize(final boolean includingBuiltIn) { - return "matrix(" + getRowsList(null).serialize(includingBuiltIn) + ")"; - } + // + // @Override + // public String serialize(final boolean includingBuiltIn) { + // return "matrix(" + getRowsList().serialize(includingBuiltIn) + ")"; + // } @Override public StreamEx stream(final IScope scope) { diff --git a/msi.gama.core/src/msi/gama/util/matrix/GamaMatrix.java b/msi.gama.core/src/msi/gama/util/matrix/GamaMatrix.java index 41d3c3b620..61248ce224 100644 --- a/msi.gama.core/src/msi/gama/util/matrix/GamaMatrix.java +++ b/msi.gama.core/src/msi/gama/util/matrix/GamaMatrix.java @@ -58,43 +58,9 @@ protected GamaPoint buildIndex(final IScope scope, final Object object) { return GamaPointType.staticCast(scope, object, false); } - public static IList getLines(final IScope scope, final IMatrix m) { - final IList result = GamaListFactory.create(Types.LIST.of(m.getGamlType().getContentType())); - for (int i = 0; i < m.getRows(scope); i++) { - result.add(getLine(scope, m, i)); - } - return result; - } - - public static IList getColumns(final IScope scope, final IMatrix m) { - final IList result = GamaListFactory.create(Types.LIST.of(m.getGamlType().getContentType())); - for (int i = 0, n = m.getCols(scope); i < n; i++) { - result.add(getColumn(scope, m, i)); - } - return result; - } - - public static IList getColumn(final IScope scope, final IMatrix m, final Integer num_col) { - final IList result = GamaListFactory.create(m.getGamlType().getContentType()); - if (num_col >= m.getCols(scope) || num_col < 0) return result; - for (int i = 0; i < m.getRows(scope); i++) { - result.add(m.get(scope, num_col, i)); - } - return result; - } - - public static IList getLine(final IScope scope, final IMatrix m, final Integer num_line) { - final IList result = GamaListFactory.create(m.getGamlType().getContentType()); - if (num_line >= m.getRows(scope) || num_line < 0) return result; - for (int i = 0; i < m.getCols(scope); i++) { - result.add(m.get(scope, i, num_line)); - } - return result; - } - @Override - public String serialize(final boolean includingBuiltIn) { - return "matrix(" + getRowsList(null).serialize(includingBuiltIn) + ")"; + public final String serialize(final boolean includingBuiltIn) { + return this.getGamlType().serialize(true) + "(" + getColumnsList().serialize(includingBuiltIn) + ")"; } public static IMatrix opPlus(final IScope scope, final IMatrix a, final IMatrix b) throws GamaRuntimeException { @@ -503,8 +469,12 @@ public final IMatrix matrixValue(final IScope scope, final IType type, final * @see msi.gama.interfaces.IMatrix#getRowsList() */ @Override - public IList> getRowsList(final IScope scope) { - return getLines(scope, this); + public IList> getRowsList() { + final IList result = GamaListFactory.create(Types.LIST.of(getGamlType().getContentType())); + for (int i = 0; i < numRows; i++) { + result.add(getRow(i)); + } + return result; } /* @@ -513,8 +483,12 @@ public IList> getRowsList(final IScope scope) { * @see msi.gama.interfaces.IMatrix#getColumnsList() */ @Override - public IList> getColumnsList(final IScope scope) { - return getColumns(scope, this); + public IList> getColumnsList() { + final IList result = GamaListFactory.create(Types.LIST.of(getGamlType().getContentType())); + for (int i = 0, n = numCols; i < n; i++) { + result.add(getColumn(i)); + } + return result; } /* @@ -523,8 +497,13 @@ public IList> getColumnsList(final IScope scope) { * @see msi.gama.interfaces.IMatrix#getRow(java.lang.Integer) */ @Override - public IList getRow(final IScope scope, final Integer num_line) { - return getLine(scope, this, num_line); + public IList getRow(final Integer n) { + final IList result = GamaListFactory.create(getGamlType().getContentType()); + if (n >= numRows || n < 0) return result; + for (int i = 0; i < numCols; i++) { + result.add(getNthElement(n * numCols + i)); + } + return result; } /* @@ -533,8 +512,13 @@ public IList getRow(final IScope scope, final Integer num_line) { * @see msi.gama.interfaces.IMatrix#getColumn(java.lang.Integer) */ @Override - public IList getColumn(final IScope scope, final Integer num_line) { - return getColumn(scope, this, num_line); + public IList getColumn(final Integer n) { + final IList result = GamaListFactory.create(getGamlType().getContentType()); + if (n >= numCols || n < 0) return result; + for (int i = 0; i < numRows; i++) { + result.add(getNthElement(i * numCols + n)); + } + return result; } /* diff --git a/msi.gama.core/src/msi/gama/util/matrix/IMatrix.java b/msi.gama.core/src/msi/gama/util/matrix/IMatrix.java index 5da2e0361b..b00a88b2f4 100644 --- a/msi.gama.core/src/msi/gama/util/matrix/IMatrix.java +++ b/msi.gama.core/src/msi/gama/util/matrix/IMatrix.java @@ -104,7 +104,7 @@ default double[] getBand(final IScope scope, final int index) { value = "rows_list(matrix([[\"el11\",\"el12\",\"el13\"],[\"el21\",\"el22\",\"el23\"],[\"el31\",\"el32\",\"el33\"]]))", equals = "[[\"el11\",\"el21\",\"el31\"],[\"el12\",\"el22\",\"el32\"],[\"el13\",\"el23\",\"el33\"]]") }, see = "columns_list") - IList> getRowsList(IScope scope); + IList> getRowsList(); @operator ( value = "columns_list", @@ -119,7 +119,7 @@ default double[] getBand(final IScope scope, final int index) { value = "columns_list(matrix([[\"el11\",\"el12\",\"el13\"],[\"el21\",\"el22\",\"el23\"],[\"el31\",\"el32\",\"el33\"]]))", equals = "[[\"el11\",\"el12\",\"el13\"],[\"el21\",\"el22\",\"el23\"],[\"el31\",\"el32\",\"el33\"]]") }, see = "rows_list") - IList> getColumnsList(IScope scope); + IList> getColumnsList(); @operator ( value = "row_at", @@ -133,7 +133,7 @@ default double[] getBand(final IScope scope, final int index) { value = "matrix([[\"el11\",\"el12\",\"el13\"],[\"el21\",\"el22\",\"el23\"],[\"el31\",\"el32\",\"el33\"]]) row_at 2", equals = "[\"el13\",\"el23\",\"el33\"]") }, see = { "column_at", "columns_list" }) - IList getRow(IScope scope, Integer num_line); + IList getRow(Integer num_line); @operator ( value = "column_at", @@ -147,7 +147,7 @@ default double[] getBand(final IScope scope, final int index) { value = "matrix([[\"el11\",\"el12\",\"el13\"],[\"el21\",\"el22\",\"el23\"],[\"el31\",\"el32\",\"el33\"]]) column_at 2", equals = "[\"el31\",\"el32\",\"el33\"]") }, see = { "row_at", "rows_list" }) - IList getColumn(IScope scope, Integer num_line); + IList getColumn(Integer num_line); @operator ( value = IKeyword.PLUS, diff --git a/msi.gama.core/src/msi/gaml/statements/CreateFromCSVDelegate.java b/msi.gama.core/src/msi/gaml/statements/CreateFromCSVDelegate.java index d4ef9be4e7..ba4f6154d9 100644 --- a/msi.gama.core/src/msi/gaml/statements/CreateFromCSVDelegate.java +++ b/msi.gama.core/src/msi/gaml/statements/CreateFromCSVDelegate.java @@ -1,12 +1,12 @@ /******************************************************************************************************* * - * msi.gaml.statements.CreateFromCSVDelegate.java, in plugin msi.gama.core, - * is part of the source code of the GAMA modeling and simulation platform (v. 1.8.1) + * msi.gaml.statements.CreateFromCSVDelegate.java, in plugin msi.gama.core, is part of the source code of the GAMA + * modeling and simulation platform (v. 1.8.1) * * (c) 2007-2020 UMI 209 UMMISCO IRD/SU & Partners * * Visit https://github.com/gama-platform/gama for license information and contacts. - * + * ********************************************************************************************************/ package msi.gaml.statements; @@ -40,33 +40,29 @@ public class CreateFromCSVDelegate implements ICreateDelegate { * @see msi.gama.common.interfaces.ICreateDelegate#acceptSource(IScope, java.lang.Object) */ @Override - public boolean acceptSource(IScope scope, final Object source) { + public boolean acceptSource(final IScope scope, final Object source) { return source instanceof GamaCSVFile; } /** - * Method createFrom() Method used to read initial values and attributes - * from a CSV values descring a synthetic population + * Method createFrom() Method used to read initial values and attributes from a CSV values descring a synthetic + * population * * @author Alexis Drogoul * @since 04-09-2012 - * @see msi.gama.common.interfaces.ICreateDelegate#createFrom(msi.gama.runtime.IScope, - * java.util.List, int, java.lang.Object) + * @see msi.gama.common.interfaces.ICreateDelegate#createFrom(msi.gama.runtime.IScope, java.util.List, int, + * java.lang.Object) */ - @SuppressWarnings("rawtypes") + @SuppressWarnings ("rawtypes") @Override public boolean createFrom(final IScope scope, final List> inits, final Integer max, final Object input, final Arguments init, final CreateStatement statement) { final GamaCSVFile source = (GamaCSVFile) input; final IExpression header = statement.getHeader(); - if (header != null) { - source.forceHeader(Cast.asBool(scope, header.value(scope))); - } + if (header != null) { source.forceHeader(Cast.asBool(scope, header.value(scope))); } final boolean hasHeader = source.hasHeader(); final IMatrix mat = source.getContents(scope); - if (mat == null || mat.isEmpty(scope)) { - return false; - } + if (mat == null || mat.isEmpty(scope)) return false; int rows = mat.getRows(scope); final int cols = mat.getCols(scope); rows = max == null ? rows : Math.min(rows, max); @@ -75,14 +71,14 @@ public boolean createFrom(final IScope scope, final List> in if (hasHeader) { headers = source.getAttributes(scope); } else { - headers = new ArrayList(); + headers = new ArrayList<>(); for (int j = 0; j < cols; j++) { headers.add(String.valueOf(j)); } } for (int i = 0; i < rows; i++) { final Map map = GamaMapFactory.create(hasHeader ? Types.STRING : Types.INT, Types.NO_TYPE); - final IList vals = mat.getRow(scope, i); + final IList vals = mat.getRow(i); for (int j = 0; j < cols; j++) { map.put(headers.get(j), vals.get(j)); } @@ -95,7 +91,7 @@ public boolean createFrom(final IScope scope, final List> in /** * Method fromFacetType() - * + * * @see msi.gama.common.interfaces.ICreateDelegate#fromFacetType() */ @Override