Util.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package holeg.power_flow;
  2. import Jama.Matrix;
  3. import java.util.ArrayList;
  4. public class Util {
  5. public static double[] subtract(double[] a, double[] b) {
  6. if (a == null)
  7. throw new IllegalArgumentException("a is null");
  8. if (b == null)
  9. throw new IllegalArgumentException("b is null");
  10. if (a.length != b.length)
  11. throw new IllegalArgumentException("a size does not match b size");
  12. double[] r = new double[a.length];
  13. for (int i = 0; i < r.length; i++)
  14. r[i] = a[i] - b[i];
  15. return r;
  16. }
  17. public static int[] indicesOf(Bus[] array, BusType value) {
  18. if (array == null)
  19. throw new IllegalArgumentException("array is null");
  20. if (array.length == 0)
  21. return new int[0];
  22. ArrayList<Integer> indices = new ArrayList<Integer>(array.length);
  23. for (int i = 0; i < array.length; i++)
  24. if (array[i].type.equals(value))
  25. indices.add(i);
  26. return indices.stream().mapToInt(i -> i).toArray();
  27. }
  28. public static Matrix fromArray(double[] array) {
  29. if (array == null)
  30. throw new IllegalArgumentException("array is null");
  31. Matrix m = new Matrix(array.length, 1);
  32. for (int i = 0; i < array.length; i++)
  33. m.set(i, 0, array[i]);
  34. return m;
  35. }
  36. public static Matrix concatColumns(Matrix a, Matrix b) {
  37. if (a == null)
  38. throw new IllegalArgumentException("a is null");
  39. if (b == null)
  40. throw new IllegalArgumentException("b is null");
  41. if (a.getRowDimension() != b.getRowDimension())
  42. throw new IllegalArgumentException("rows count do not match");
  43. Matrix c = new Matrix(a.getRowDimension(), a.getColumnDimension() + b.getColumnDimension());
  44. for (int i = 0; i < a.getRowDimension(); i++)
  45. for (int j = 0; j < a.getColumnDimension(); j++)
  46. c.set(i, j, a.get(i, j));
  47. for (int i = 0; i < b.getRowDimension(); i++)
  48. for (int j = 0; j < b.getColumnDimension(); j++)
  49. c.set(i, j + a.getColumnDimension(), b.get(i, j));
  50. return c;
  51. }
  52. public static Matrix concatRows(Matrix a, Matrix b) {
  53. if (a == null)
  54. throw new IllegalArgumentException("a is null");
  55. if (b == null)
  56. throw new IllegalArgumentException("b is null");
  57. if (a.getColumnDimension() != b.getColumnDimension())
  58. throw new IllegalArgumentException("columns count do not match");
  59. Matrix c = new Matrix(a.getRowDimension() + b.getRowDimension(), a.getColumnDimension());
  60. for (int i = 0; i < a.getRowDimension(); i++)
  61. for (int j = 0; j < a.getColumnDimension(); j++)
  62. c.set(i, j, a.get(i, j));
  63. for (int i = 0; i < b.getRowDimension(); i++)
  64. for (int j = 0; j < b.getColumnDimension(); j++)
  65. c.set(i + a.getRowDimension(), j, b.get(i, j));
  66. return c;
  67. }
  68. }