Skip to content

Commit

Permalink
The serialization of matrices wrongfully displayed the list of rows
Browse files Browse the repository at this point in the history
(while the reverse operation, the creation of a matrix from a list,
expects a list of columns)
  • Loading branch information
AlexisDrogoul committed Aug 3, 2021
1 parent c7cc59f commit 5ed78e8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 79 deletions.
14 changes: 7 additions & 7 deletions msi.gama.core/src/msi/gama/util/matrix/GamaFloatMatrix.java
Expand Up @@ -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;
}
Expand Down Expand Up @@ -459,19 +459,19 @@ 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];
}

@Override
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<float>(" + getRowsList(null).serialize(includingBuiltIn) + ")";
}
//
// @Override
// public String serialize(final boolean includingBuiltIn) {
// return "matrix<float>(" + getRowsList(null).serialize(includingBuiltIn) + ")";
// }

@Override
public IContainerType getGamlType() {
Expand Down
12 changes: 6 additions & 6 deletions msi.gama.core/src/msi/gama/util/matrix/GamaIntMatrix.java
Expand Up @@ -502,19 +502,19 @@ 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];
}

@Override
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<int>(" + getRowsList(null).serialize(includingBuiltIn) + ")";
}
//
// @Override
// public String serialize(final boolean includingBuiltIn) {
// return "matrix<int>(" + getRowsList().serialize(includingBuiltIn) + ")";
// }

@Override
public StreamEx<Integer> stream(final IScope scope) {
Expand Down
72 changes: 28 additions & 44 deletions msi.gama.core/src/msi/gama/util/matrix/GamaMatrix.java
Expand Up @@ -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 {
Expand Down Expand Up @@ -503,8 +469,12 @@ public final IMatrix<T> matrixValue(final IScope scope, final IType type, final
* @see msi.gama.interfaces.IMatrix#getRowsList()
*/
@Override
public IList<IList<T>> getRowsList(final IScope scope) {
return getLines(scope, this);
public IList<IList<T>> getRowsList() {
final IList result = GamaListFactory.create(Types.LIST.of(getGamlType().getContentType()));
for (int i = 0; i < numRows; i++) {
result.add(getRow(i));
}
return result;
}

/*
Expand All @@ -513,8 +483,12 @@ public IList<IList<T>> getRowsList(final IScope scope) {
* @see msi.gama.interfaces.IMatrix#getColumnsList()
*/
@Override
public IList<IList<T>> getColumnsList(final IScope scope) {
return getColumns(scope, this);
public IList<IList<T>> 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;
}

/*
Expand All @@ -523,8 +497,13 @@ public IList<IList<T>> getColumnsList(final IScope scope) {
* @see msi.gama.interfaces.IMatrix#getRow(java.lang.Integer)
*/
@Override
public IList<T> getRow(final IScope scope, final Integer num_line) {
return getLine(scope, this, num_line);
public IList<T> 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;
}

/*
Expand All @@ -533,8 +512,13 @@ public IList<T> getRow(final IScope scope, final Integer num_line) {
* @see msi.gama.interfaces.IMatrix#getColumn(java.lang.Integer)
*/
@Override
public IList<T> getColumn(final IScope scope, final Integer num_line) {
return getColumn(scope, this, num_line);
public IList<T> 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;
}

/*
Expand Down
8 changes: 4 additions & 4 deletions msi.gama.core/src/msi/gama/util/matrix/IMatrix.java
Expand Up @@ -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<IList<T>> getRowsList(IScope scope);
IList<IList<T>> getRowsList();

@operator (
value = "columns_list",
Expand All @@ -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<IList<T>> getColumnsList(IScope scope);
IList<IList<T>> getColumnsList();

@operator (
value = "row_at",
Expand All @@ -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<T> getRow(IScope scope, Integer num_line);
IList<T> getRow(Integer num_line);

@operator (
value = "column_at",
Expand All @@ -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<T> getColumn(IScope scope, Integer num_line);
IList<T> getColumn(Integer num_line);

@operator (
value = IKeyword.PLUS,
Expand Down
32 changes: 14 additions & 18 deletions 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;

Expand Down Expand Up @@ -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<Map<String, Object>> 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);
Expand All @@ -75,14 +71,14 @@ public boolean createFrom(final IScope scope, final List<Map<String, Object>> in
if (hasHeader) {
headers = source.getAttributes(scope);
} else {
headers = new ArrayList<String>();
headers = new ArrayList<>();
for (int j = 0; j < cols; j++) {
headers.add(String.valueOf(j));
}
}
for (int i = 0; i < rows; i++) {
final Map<String, Object> 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));
}
Expand All @@ -95,7 +91,7 @@ public boolean createFrom(final IScope scope, final List<Map<String, Object>> in

/**
* Method fromFacetType()
*
*
* @see msi.gama.common.interfaces.ICreateDelegate#fromFacetType()
*/
@Override
Expand Down

0 comments on commit 5ed78e8

Please sign in to comment.