ComplexMatrix.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package holeg.power_flow;
  2. import Jama.Matrix;
  3. import java.io.PrintStream;
  4. import java.util.Arrays;
  5. public class ComplexMatrix {
  6. private final int width;
  7. private final int height;
  8. private final ComplexNumber[] data;
  9. public ComplexMatrix(int size) {
  10. this(size, size);
  11. }
  12. public ComplexMatrix(int width, int height) {
  13. if (width <= 0)
  14. throw new IllegalArgumentException("width is invalid");
  15. if (height <= 0)
  16. throw new IllegalArgumentException("height is invalid");
  17. this.width = width;
  18. this.height = height;
  19. this.data = new ComplexNumber[width * height];
  20. Arrays.fill(this.data, ComplexNumber.Zero);
  21. }
  22. public static ComplexMatrix sameSizeAs(ComplexMatrix other) {
  23. return new ComplexMatrix(other.getWidth(), other.getHeight());
  24. }
  25. public static ComplexMatrix fromArray(ComplexNumber[] numbers) {
  26. if (numbers == null)
  27. throw new IllegalArgumentException("number is null");
  28. if (numbers.length == 0)
  29. throw new IllegalArgumentException("number is empty");
  30. ComplexMatrix out = new ComplexMatrix(numbers.length, 1);
  31. for (int i = 0; i < numbers.length; i++)
  32. out.uncheckedSet(i, 0, numbers[i]);
  33. return out;
  34. }
  35. public int getWidth() {
  36. return width;
  37. }
  38. public int getHeight() {
  39. return height;
  40. }
  41. private ComplexNumber uncheckedGet(int row, int column) {
  42. return data[column * width + row];
  43. }
  44. public ComplexNumber get(int row, int column) {
  45. if (row < 0 || row >= width)
  46. throw new IllegalArgumentException("row is invalid");
  47. if (column < 0 || column >= height)
  48. throw new IllegalArgumentException("column is invalid");
  49. return uncheckedGet(row, column);
  50. }
  51. public void set(int row, int column, double realValue) {
  52. set(row, column, new ComplexNumber(realValue, 0));
  53. }
  54. private void uncheckedSet(int row, int column, ComplexNumber value) {
  55. data[column * width + row] = value;
  56. }
  57. public void set(int row, int column, ComplexNumber value) {
  58. if (row < 0 || row >= width)
  59. throw new IllegalArgumentException("row is invalid");
  60. if (column < 0 || column >= height)
  61. throw new IllegalArgumentException("column is invalid");
  62. if (value == null)
  63. throw new IllegalArgumentException("value is null");
  64. uncheckedSet(row, column, value);
  65. }
  66. public Matrix getRealMatrix() {
  67. Matrix m = new Matrix(getWidth(), getHeight());
  68. for (int i = 0; i < getWidth(); i++)
  69. for (int j = 0; j < getHeight(); j++)
  70. m.set(i, j, uncheckedGet(i, j).real);
  71. return m;
  72. }
  73. public Matrix getImaginaryMatrix() {
  74. Matrix m = new Matrix(getWidth(), getHeight());
  75. for (int i = 0; i < getWidth(); i++)
  76. for (int j = 0; j < getHeight(); j++)
  77. m.set(i, j, uncheckedGet(i, j).imaginary);
  78. return m;
  79. }
  80. public ComplexMatrix add(ComplexMatrix other) {
  81. if (getWidth() != other.getWidth())
  82. throw new IllegalArgumentException("width does not match");
  83. if (getHeight() != other.getHeight())
  84. throw new IllegalArgumentException("height does not match");
  85. ComplexMatrix out = new ComplexMatrix(getWidth(), getHeight());
  86. for (int i = 0; i < getWidth(); i++)
  87. for (int j = 0; j < getHeight(); j++)
  88. out.uncheckedSet(i, j, uncheckedGet(i, j).add(other.uncheckedGet(i, j)));
  89. return out;
  90. }
  91. public ComplexMatrix subtract(ComplexMatrix other) {
  92. if (getWidth() != other.getWidth())
  93. throw new IllegalArgumentException("width does not match");
  94. if (getHeight() != other.getHeight())
  95. throw new IllegalArgumentException("height does not match");
  96. ComplexMatrix out = new ComplexMatrix(getWidth(), getHeight());
  97. for (int i = 0; i < getWidth(); i++)
  98. for (int j = 0; j < getHeight(); j++)
  99. out.uncheckedSet(i, j, uncheckedGet(i, j).subtract(other.uncheckedGet(i, j)));
  100. return out;
  101. }
  102. public ComplexMatrix multiply(ComplexNumber[] numbers) {
  103. return multiply(ComplexMatrix.fromArray(numbers));
  104. }
  105. public ComplexMatrix multiply(ComplexMatrix other) {
  106. if (getHeight() != other.getWidth())
  107. throw new IllegalArgumentException("height does not match other width");
  108. ComplexMatrix out = new ComplexMatrix(getWidth(), other.getHeight());
  109. for (int i = 0; i < getWidth(); i++) {
  110. for (int j = 0; j < other.getHeight(); j++) {
  111. ComplexNumber sum = ComplexNumber.Zero;
  112. for (int k = 0; k < getHeight(); k++) {
  113. ComplexNumber a = uncheckedGet(i, k);
  114. ComplexNumber b = other.uncheckedGet(k, j);
  115. sum = sum.add(a.multiply(b));
  116. }
  117. out.uncheckedSet(i, j, sum);
  118. }
  119. }
  120. return out;
  121. }
  122. public void printTo(PrintStream stream) {
  123. if (stream == null)
  124. throw new IllegalArgumentException("stream is null");
  125. for (int row = 0; row < getWidth(); row++) {
  126. for (int col = 0; col < getHeight(); col++)
  127. stream.printf("%s, ", uncheckedGet(row, col).toString());
  128. stream.println();
  129. }
  130. }
  131. }