JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
Util.java
Go to the documentation of this file.
1 package jarmos;
2 
3 import java.util.Arrays;
4 import java.util.Random;
5 
6 import org.apache.commons.math.linear.RealMatrix;
7 import org.apache.commons.math.linear.RealVector;
8 
15 public class Util {
16  public static double[] linspace(double a, double b, int num) {
17  double[] res = new double[num];
18  double step = (b - a) / (num - 2);
19  for (int i = 0; i < num - 1; i++) {
20  res[i] = a + i * step;
21  }
22  res[num - 1] = b;
23  return res;
24  }
25 
36  public static double[] range(double a, double step, double b) {
37  // int num = (int) Math.ceil((b - a) / step);
38  if (a > b)
39  return null;
40  int num = 1;
41  double hlp = a + step;
42  while (hlp <= b) {
43  hlp += step;
44  num++;
45  }
46  double[] res = new double[num];
47  for (int i = 0; i < num; i++) {
48  res[i] = a + i * step;
49  }
50  return res;
51  }
52 
53  public static void vecAdd(double[] to, double[] what) {
54  for (int i = 0; i < to.length; i++) {
55  to[i] += what[i];
56  }
57  }
58 
59  public static RealVector randVector(int dim) {
60  Random r = new Random();
61  r.setSeed(System.currentTimeMillis());
62  RealVector res = MathFactory.createRealVector(dim);
63  for (int i = 0; i < dim; i++) {
64  res.setEntry(i, r.nextDouble());
65  }
66  return res;
67  }
68 
69  public static RealMatrix randMatrix(int rows, int cols) {
70  RealMatrix res = MathFactory.createRealMatrix(rows, cols);
71  for (int i = 0; i < rows; i++) {
72  res.setRow(i, randVector(cols).getData());
73  }
74  return res;
75  }
76 
77  public static String realMatToString(RealMatrix r) {
78  String res = "RealMatrix(" + r.getRowDimension() + "x" + r.getColumnDimension() + ") of type '"
79  + r.getClass().getName() + "'\n";
80  for (int i = 0; i < r.getRowDimension(); i++) {
81  res += Arrays.toString(r.getRow(i)) + "\n";
82  }
83  return res;
84  }
85 
86  public static String realVecToString(RealVector r) {
87  String res = "RealVector(" + r.getDimension() + ") of type '" + r.getClass().getName() + "'\n";
88  return res += Arrays.toString(r.getData()) + "\n";
89  }
90 
91 }
static RealVector randVector(int dim)
Definition: Util.java:59
static double[] range(double a, double step, double b)
The Java equivalent to Matlab&#39;s &quot;range = a:step:b&quot;.
Definition: Util.java:36
static double[] linspace(double a, double b, int num)
Definition: Util.java:16
static void vecAdd(double[] to, double[] what)
Definition: Util.java:53
static RealMatrix randMatrix(int rows, int cols)
Definition: Util.java:69
static String realMatToString(RealMatrix r)
Definition: Util.java:77
static String realVecToString(RealVector r)
Definition: Util.java:86
Utility functions.
Definition: Util.java:15