-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added saving and reading nn-data to/from JSON-file
- Loading branch information
Kim Feichtinger
authored and
Kim Feichtinger
committed
Mar 7, 2018
1 parent
dd26e18
commit 9e49497
Showing
5 changed files
with
151 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.idea | ||
target | ||
out | ||
artifacts | ||
artifacts | ||
nn_data.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package utilities; | ||
|
||
import org.ejml.simple.SimpleMatrix; | ||
import org.json.simple.JSONObject; | ||
|
||
/** | ||
* Created by KimFeichtinger on 07.03.18. | ||
*/ | ||
public class MatrixConverter { | ||
|
||
public static SimpleMatrix arrayToMatrix(double[] i){ | ||
double[][] input = {i}; | ||
return new SimpleMatrix(input).transpose(); | ||
} | ||
|
||
public static double[][] matrixToArray(SimpleMatrix i){ | ||
double[][] result = new double[i.numRows()][i.numCols()]; | ||
for (int j = 0; j < result.length; j++) { | ||
for (int k = 0; k < result[0].length; k++) { | ||
result[j][k] = i.get(j, k); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public static SimpleMatrix jsonToMatrix(JSONObject i){ | ||
int rows = i.size(); | ||
int cols = ((JSONObject) i.get(Integer.toString(0))).size(); | ||
|
||
SimpleMatrix result = new SimpleMatrix(rows, cols); | ||
|
||
for (int j = 0; j < i.size(); j++) { | ||
JSONObject js = (JSONObject) i.get(Integer.toString(j)); | ||
for (int k = 0; k < js.size(); k++) { | ||
double d = (double) js.get(Integer.toString(k)); | ||
result.set(j, k, d); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static JSONObject matrixToJSON(SimpleMatrix i){ | ||
JSONObject result = new JSONObject(); | ||
JSONObject a = new JSONObject(); | ||
|
||
for (int j = 0; j < i.numRows(); j++) { | ||
a.clear(); | ||
for (int k = 0; k < i.numCols(); k++) { | ||
a.put(k, i.get(j, k)); | ||
} | ||
result.put(j, new JSONObject(a)); | ||
} | ||
|
||
return result; | ||
} | ||
} |