123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- package holeg.power_flow;
- import Jama.Matrix;
- import java.util.ArrayList;
- public class Util {
- public static double[] subtract(double[] a, double[] b) {
- if (a == null)
- throw new IllegalArgumentException("a is null");
- if (b == null)
- throw new IllegalArgumentException("b is null");
- if (a.length != b.length)
- throw new IllegalArgumentException("a size does not match b size");
- double[] r = new double[a.length];
- for (int i = 0; i < r.length; i++)
- r[i] = a[i] - b[i];
- return r;
- }
- public static int[] indicesOf(Bus[] array, BusType value) {
- if (array == null)
- throw new IllegalArgumentException("array is null");
- if (array.length == 0)
- return new int[0];
- ArrayList<Integer> indices = new ArrayList<Integer>(array.length);
- for (int i = 0; i < array.length; i++)
- if (array[i].type.equals(value))
- indices.add(i);
- return indices.stream().mapToInt(i -> i).toArray();
- }
- public static Matrix fromArray(double[] array) {
- if (array == null)
- throw new IllegalArgumentException("array is null");
- Matrix m = new Matrix(array.length, 1);
- for (int i = 0; i < array.length; i++)
- m.set(i, 0, array[i]);
- return m;
- }
- public static Matrix concatColumns(Matrix a, Matrix b) {
- if (a == null)
- throw new IllegalArgumentException("a is null");
- if (b == null)
- throw new IllegalArgumentException("b is null");
- if (a.getRowDimension() != b.getRowDimension())
- throw new IllegalArgumentException("rows count do not match");
- Matrix c = new Matrix(a.getRowDimension(), a.getColumnDimension() + b.getColumnDimension());
- for (int i = 0; i < a.getRowDimension(); i++)
- for (int j = 0; j < a.getColumnDimension(); j++)
- c.set(i, j, a.get(i, j));
- for (int i = 0; i < b.getRowDimension(); i++)
- for (int j = 0; j < b.getColumnDimension(); j++)
- c.set(i, j + a.getColumnDimension(), b.get(i, j));
- return c;
- }
- public static Matrix concatRows(Matrix a, Matrix b) {
- if (a == null)
- throw new IllegalArgumentException("a is null");
- if (b == null)
- throw new IllegalArgumentException("b is null");
- if (a.getColumnDimension() != b.getColumnDimension())
- throw new IllegalArgumentException("columns count do not match");
- Matrix c = new Matrix(a.getRowDimension() + b.getRowDimension(), a.getColumnDimension());
- for (int i = 0; i < a.getRowDimension(); i++)
- for (int j = 0; j < a.getColumnDimension(); j++)
- c.set(i, j, a.get(i, j));
- for (int i = 0; i < b.getRowDimension(); i++)
- for (int j = 0; j < b.getColumnDimension(); j++)
- c.set(i + a.getRowDimension(), j, b.get(i, j));
- return c;
- }
- }
|