Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.22 KB | None | 0 0
  1. package pl.kul.gaweda;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5.  
  6. public class Matrix {
  7.  
  8. protected double[] _matrix = null;
  9. private int _rowCount = 0;
  10. private int _colCount = 0;
  11. private ArrayList _matrixList = null;
  12. private Random _rng = new Random();
  13.  
  14. /**
  15. *Initialises the matrix based on the specified array.
  16. * The matrix created will contain a single row.
  17. * @param array
  18. */
  19. public Matrix(double[] array) {
  20. int rows = 1;
  21. int cols = array.length;
  22.  
  23. if (cols < 1) {
  24. throw new ArrayIndexOutOfBoundsException(cols);
  25. }
  26.  
  27. //set the properties
  28. _rowCount = rows;
  29. _colCount = cols;
  30.  
  31. _matrix = array;
  32.  
  33. }
  34.  
  35. /**
  36. * Initialises the matrix based on the specified array.
  37. * The matrix created will contain a single row.
  38. * @param array
  39. */
  40. public Matrix(boolean[] array) {
  41. int rows = 1;
  42. int cols = array.length;
  43.  
  44. if (cols < 1) {
  45. throw new ArrayIndexOutOfBoundsException(cols);
  46. }
  47.  
  48. //set the properties
  49. _rowCount = rows;
  50. _colCount = cols;
  51.  
  52. _matrix = ConvertToDoubleBool(array);
  53.  
  54. }
  55.  
  56. /**
  57. * Initialises the matrix based on the specified array.
  58. * If the columnMatrix parameter is set to true,
  59. * the matrix created will contain a single column.
  60. * @param array
  61. * @param columnMatrix
  62. *
  63. */
  64. public Matrix(double[] array, boolean columnMatrix) {
  65. int cols = 1;
  66. int rows = 1;
  67.  
  68. if (columnMatrix) {
  69. rows = array.length;
  70. if (rows < 1) {
  71. throw new ArrayIndexOutOfBoundsException(rows);
  72. }
  73. } else {
  74. cols = array.length;
  75. if (cols < 1) {
  76. throw new ArrayIndexOutOfBoundsException(rows);
  77. }
  78. }
  79.  
  80. //set the properties
  81. _rowCount = rows;
  82. _colCount = cols;
  83.  
  84. _matrix = array;
  85. }
  86.  
  87. /**
  88. * Initialises the matrix based on the specified array.
  89. * If the columnMatrix parameter is set to false, the matrix created will contain a single row.
  90. * If the coulmnMatrix parameter is set to true, the matrix created will contain a single column.
  91. * @param array
  92. * @param columnMatrix
  93. */
  94. public Matrix(boolean[] array, boolean columnMatrix) {
  95. int cols = 1;
  96. int rows = 1;
  97.  
  98. if (columnMatrix) {
  99. rows = array.length;
  100. if (rows < 1) {
  101. throw new ArrayIndexOutOfBoundsException(rows);
  102. }
  103. } else {
  104. cols = array.length;
  105. if (cols < 1) {
  106. throw new ArrayIndexOutOfBoundsException(cols);
  107. }
  108. }
  109.  
  110. //set the properties
  111. _rowCount = rows;
  112. _colCount = cols;
  113.  
  114. _matrix = ConvertToDoubleBool(array);
  115. }
  116.  
  117. /**
  118. * Initialises the matrix based on the specified array. Dimension 0 of the array will
  119. * be represented as Rows and dimension 1 of the array will be represented as Columns.
  120. * @param array
  121. */
  122. public Matrix(double[][] array) {
  123. int rows = array.length;
  124. int cols = array[0].length;
  125.  
  126. if (rows < 1) {
  127. throw new ArrayIndexOutOfBoundsException(rows);
  128. }
  129. if (cols < 1) {
  130. throw new ArrayIndexOutOfBoundsException(cols);
  131. }
  132.  
  133. //set the properties
  134. _rowCount = rows;
  135. _colCount = cols;
  136.  
  137. //populate the matrix (cant use array copy as the arrays are different dimensions)
  138. _matrix = new double[rows * cols];
  139. for (int row = 0; row < rows; row++) {
  140. for (int col = 0; col < cols; col++) {
  141. _matrix[row * cols + col] = array[row][col];
  142. }
  143. }
  144. }
  145.  
  146. /**
  147. * Initialises the matrix based on the specified array. Dimension 0 of the array will
  148. * be represented as Rows and dimension 1 of the array will be represented as Columns.
  149. * @param array
  150. */
  151. public Matrix(boolean[][] array) {
  152. int rows = array.length;
  153. int cols = array[0].length;
  154.  
  155. if (rows < 1) {
  156. throw new ArrayIndexOutOfBoundsException(rows);
  157. }
  158. if (cols < 1) {
  159. throw new ArrayIndexOutOfBoundsException(cols);
  160. }
  161.  
  162. //set the properties
  163. _rowCount = rows;
  164. _colCount = cols;
  165.  
  166. //populate the matrix (cant use array copy as the arrays are different dimensions)
  167. _matrix = new double[rows * cols];
  168. for (int row = 0; row < rows; row++) {
  169. for (int col = 0; col < cols; col++) {
  170. if (array[row][col]) {
  171. _matrix[row * cols + col] = 1.0;
  172. } else {
  173. _matrix[row * cols + col] = 0;
  174. }
  175. }
  176. }
  177. }
  178.  
  179. /**
  180. * Initialises the matrix based on the specified array, column and row parameters.
  181. * @param array
  182. * @param rows
  183. * @param cols
  184. */
  185. public Matrix(double[] array, int rows, int cols) {
  186.  
  187. if (rows < 1) {
  188. throw new ArrayIndexOutOfBoundsException(rows);
  189. }
  190. if (cols < 1) {
  191. throw new ArrayIndexOutOfBoundsException(cols);
  192. }
  193.  
  194. //set the properties
  195. _rowCount = rows;
  196. _colCount = cols;
  197.  
  198. _matrix = new double[rows * cols];
  199.  
  200. if (_matrix.length == array.length) {
  201. _matrix = array;
  202. } else {
  203. throw new IllegalArgumentException("ArgumentVectorNotValid");
  204. }
  205.  
  206. }
  207.  
  208.  
  209. /**
  210. * Initialises the matrix to the specified size. All elements are set to zero.
  211. * @param rows
  212. * @param cols
  213. */
  214. public Matrix(int rows, int cols) {
  215. if (rows < 1) {
  216. throw new ArrayIndexOutOfBoundsException(rows);
  217. }
  218. if (cols < 1) {
  219. throw new ArrayIndexOutOfBoundsException(cols);
  220. }
  221.  
  222. //set the properties
  223. _rowCount = rows;
  224. _colCount = cols;
  225.  
  226. //store the whole thing in a single array
  227. //one row at a time for example the matrix
  228. //
  229. // 1 2 3
  230. // 6 7 8
  231. //
  232. //would be stored as follows
  233. //
  234. // 1 2 3 6 7 8
  235. _matrix = new double[rows * cols];
  236.  
  237. for (int index = 0; index < _matrix.length; index++) {
  238. _matrix[index] = 0.0;
  239. }
  240.  
  241. }
  242.  
  243. /**
  244. * Initialises the matrix to the specified size, setting all cells to the
  245. * specified value.
  246. * @param rows
  247. * @param cols
  248. * @param value
  249. */
  250. public Matrix(int rows, int cols, double value) {
  251. if (rows < 1) {
  252. throw new ArrayIndexOutOfBoundsException(rows);
  253. }
  254. if (cols < 1) {
  255. throw new ArrayIndexOutOfBoundsException(cols);
  256. }
  257.  
  258. //set the properties
  259. _rowCount = rows;
  260. _colCount = cols;
  261.  
  262. //store the whole thing in a single array
  263. //one row at a time for example the matrix
  264. //
  265. // 1 2 3
  266. // 6 7 8
  267. //
  268. //would be stored as follows
  269. //
  270. // 1 2 3 6 7 8
  271. _matrix = new double[rows * cols];
  272.  
  273. for (int index = 0; index < _matrix.length; index++) {
  274. _matrix[index] = value;
  275. }
  276.  
  277. }
  278.  
  279. /**
  280. * Initialises the matrix to the specified size. If the addRandomValues parameter
  281. * is set to true, all elements are set to a random value between 0 and 1.
  282. * @param rows
  283. * @param cols
  284. * @param addRandomValues
  285. */
  286. public Matrix(int rows, int cols, boolean addRandomValues) {
  287. if (rows < 1) {
  288. throw new ArrayIndexOutOfBoundsException(rows);
  289. }
  290. if (cols < 1) {
  291. throw new ArrayIndexOutOfBoundsException(cols);
  292. }
  293.  
  294. //set the properties
  295. _rowCount = rows;
  296. _colCount = cols;
  297.  
  298. //store the whole thing in a single array
  299. //one row at a time for example the matrix
  300. //
  301. // 1 2 3
  302. // 6 7 8
  303. //
  304. //would be stored as follows
  305. //
  306. // 1 2 3 6 7 8
  307. _matrix = new double[rows * cols];
  308.  
  309. if (addRandomValues) {
  310. for (int index = 0; index < _matrix.length; index++) {
  311. _matrix[index] = _rng.nextDouble();
  312. }
  313. } else {
  314. for (int index = 0; index < _matrix.length; index++) {
  315. _matrix[index] = 0.0;
  316. }
  317. }
  318. }
  319.  
  320. /**
  321. * Create a square diagonal matrix. Cells are initialised to zero with the exception of those
  322. * on the diagonal (Top Left to Bottom Right), which are set to the value specified.
  323. * @param diagonalMatrixSize
  324. * @param diagonalValue
  325. */
  326.  
  327. public Matrix(int diagonalMatrixSize, double diagonalValue) {
  328.  
  329. if (diagonalMatrixSize < 1) {
  330. throw new ArrayIndexOutOfBoundsException("diagonalMatrixSize < 1");
  331. }
  332.  
  333. _matrix = new double[diagonalMatrixSize * diagonalMatrixSize];
  334.  
  335. _rowCount = diagonalMatrixSize;
  336. _colCount = diagonalMatrixSize;
  337.  
  338. int row = -1;
  339.  
  340. for (int index = 0; index < _matrix.length; index++) {
  341.  
  342. if (index % this.ColumnCount() == 0) {
  343. row++;
  344. }
  345.  
  346. if (index == (row * this.ColumnCount()) + row) {
  347. _matrix[index] = diagonalValue;
  348. } else {
  349. _matrix[index] = 0.0;
  350. }
  351. }
  352. }
  353.  
  354. /**
  355. * Create a square unit matrix. Cells are initialised to zero with the exception of those
  356. * on the diagonal (Top Left to Bottom Right), which are set to 1.
  357. * @param unitMatrixSize
  358. */
  359. public Matrix(int unitMatrixSize) {
  360.  
  361. if (unitMatrixSize < 1) {
  362. throw new ArrayIndexOutOfBoundsException("unitMatrixSize < 1");
  363. }
  364.  
  365. _matrix = new double[unitMatrixSize * unitMatrixSize];
  366.  
  367. _rowCount = unitMatrixSize;
  368. _colCount = unitMatrixSize;
  369.  
  370. int row = -1;
  371.  
  372. for (int index = 0; index < _matrix.length; index++) {
  373.  
  374. if (index % this.ColumnCount() == 0) {
  375. row++;
  376. }
  377.  
  378. if (index == (row * this.ColumnCount()) + row) {
  379. _matrix[index] = 1.0;
  380. } else {
  381. _matrix[index] = 0.0;
  382. }
  383. }
  384. }
  385.  
  386. /**
  387. * Returns an integer representing the number of rows in the matrix.
  388. * @return
  389. */
  390. public int RowCount() {
  391. return _rowCount;
  392. }
  393.  
  394. /**
  395. * Returns an integer representing the number of columns in the matrix.
  396. * @return
  397. */
  398. public int ColumnCount() {
  399. return _colCount;
  400. }
  401.  
  402. /**
  403. * Returns true if the matrix consists of a single row or a single column.
  404. * @return
  405. */
  406. public boolean IsVector() {
  407. return this.ColumnCount() == 1 || this.RowCount() == 1;
  408. }
  409.  
  410. /**
  411. * Returns true if the matrix consists of a single row.
  412. * @return
  413. */
  414. public boolean IsRowVector() {
  415. return this.RowCount() == 1;
  416. }
  417.  
  418. /**
  419. * Returns true if the matrix consists of a single column.
  420. * @return
  421. */
  422. public boolean IsColumnVector() {
  423. return this.ColumnCount() == 1;
  424. }
  425.  
  426. /**
  427. * Returns the number of cells in the matrix.
  428. * @return
  429. */
  430. public int length() {
  431. return _matrix.length;
  432. }
  433.  
  434. /**
  435. * Returns the total of the values of all elements of the matrix.
  436. * @return
  437. */
  438. public double Sum() {
  439. double result = 0.0;
  440. for (int index = 0; index < _matrix.length; index++) {
  441. result += _matrix[index];
  442. }
  443. return result;
  444. }
  445.  
  446. /**
  447. * Returns the square root of the sum of the squared elements.
  448. * @return
  449. */
  450. public double Vectorlength() {
  451. if (!this.IsVector()) {
  452. throw new IllegalArgumentException("ArgumentMatrixIsNotVector");
  453. }
  454.  
  455. double result = 0.0;
  456.  
  457. for (int index = 0; index < _matrix.length; index++) {
  458. result += Math.pow(_matrix[index], 2);
  459. }
  460. return Math.sqrt(result);
  461.  
  462. }
  463.  
  464. /**
  465. * Returns a Matrix containing the specified rows.
  466. * @param startRow
  467. * @param endRow
  468. * @return
  469. */
  470. public Matrix GetRows(int startRow, int endRow) {
  471.  
  472. //validate arguments
  473. if (startRow >= endRow) {
  474. throw new ArrayIndexOutOfBoundsException("ArgumentStartRowGreaterThanEndRow");
  475. }
  476. if (startRow >= this.RowCount() || endRow >= this.RowCount() || startRow < 0 || endRow < 0) {
  477. throw new ArrayIndexOutOfBoundsException("ArgumentInvalidRow");
  478. }
  479.  
  480. //get rows to return and number of columns in a row
  481. int rowsToReturn = endRow - startRow + 1;
  482.  
  483. //create a matrix for the result
  484. Matrix result = new Matrix(rowsToReturn, this.ColumnCount());
  485.  
  486. //populate the result
  487. System.arraycopy(this._matrix, startRow * this.ColumnCount(), result._matrix, 0, rowsToReturn * this.ColumnCount());
  488. //create the Matrix to return
  489. return result;
  490. }
  491.  
  492. /**
  493. * Returns a RowMatrix containing the specified row.
  494. * @param row
  495. * @return
  496. */
  497. public Matrix GetRow(int row) {
  498. //validate arguments
  499. if (row >= this.RowCount() || row < 0) {
  500. throw new ArrayIndexOutOfBoundsException("ArgumentInvalidRow");
  501. }
  502.  
  503. double[] selectedRow = new double[this.ColumnCount()];
  504. System.arraycopy(_matrix, row * this.ColumnCount(), selectedRow, 0, this.ColumnCount());
  505.  
  506. //get number of elements in each Row (i.e. how many columns it has)
  507. Matrix result = new Matrix(1, this.ColumnCount());
  508. result._matrix = selectedRow;
  509.  
  510. return result;
  511. }
  512.  
  513. /**
  514. * Returns a Matrix containing the specified columns.
  515. * @param startColumn
  516. * @param endColumn
  517. * @return
  518. */
  519. public Matrix GetColumns(int startColumn, int endColumn) {
  520. //validate arguments
  521. if (startColumn >= endColumn) {
  522. throw new ArrayIndexOutOfBoundsException("ArgumentStartColumnGreaterThanEndRow");
  523. }
  524. if (startColumn >= this.ColumnCount() || endColumn >= this.ColumnCount() || startColumn < 0 || endColumn < 0) {
  525. throw new ArrayIndexOutOfBoundsException("ArgumentInvalidColumn");
  526. }
  527.  
  528. //get rows to return and number of columns in a row
  529. int colsToReturn = endColumn - startColumn + 1;
  530.  
  531. //create a matrix for the result
  532. Matrix result = new Matrix(this.RowCount(), colsToReturn);
  533.  
  534. //populate the result
  535. for (int row = 0; row < this.RowCount(); row++) {
  536. System.arraycopy(_matrix, (row * this.ColumnCount()) + startColumn, result._matrix, row * colsToReturn, colsToReturn);
  537. }
  538. return result;
  539. }
  540.  
  541. /**
  542. * Returns a ColumnMatrix containing the specified coulmn.
  543. * @param column
  544. * @return
  545. */
  546. public Matrix GetColumn(int column) {
  547.  
  548. //validate arguments
  549. if (column >= this.ColumnCount() || column < 0) {
  550. throw new ArrayIndexOutOfBoundsException("ArgumentInvalidColumn:" + column);
  551. }
  552.  
  553. double[] selectedColumn = new double[this.RowCount()];
  554. for (int index = 0; index < this.RowCount(); index++) {
  555. selectedColumn[index] = _matrix[(this.ColumnCount() * index) + column];
  556.  
  557. }
  558.  
  559. //get number of elements in each Row (i.e. how many columns it has)
  560. Matrix result = new Matrix(this.RowCount(), 1);
  561. result._matrix = selectedColumn;
  562.  
  563. return result;
  564.  
  565. }
  566.  
  567. /**
  568. * Returns the contents of the specified matrix element.
  569. * @param row
  570. * @param column
  571. * @return
  572. */
  573. public double GetElement(int row, int column) {
  574. //validate arguments
  575. if (row >= this.RowCount() || row < 0) {
  576. throw new ArrayIndexOutOfBoundsException("ArgumentInvalidRow:" + row);
  577. }
  578. if (column >= this.ColumnCount() || column < 0) {
  579. throw new ArrayIndexOutOfBoundsException("ArgumentInvalidColumn:" + column);
  580. }
  581.  
  582. return _matrix[(row * this.ColumnCount()) + column];
  583. }
  584.  
  585. /**
  586. * Returns the contents of the specified matrix element.
  587. * @param row
  588. * @param column
  589. * @param value
  590. */
  591. public void SetElement(int row, int column, double value) {
  592. //validate arguments
  593. if (row >= this.RowCount() || row < 0) {
  594. throw new ArrayIndexOutOfBoundsException("ArgumentInvalidRow:" + row);
  595. }
  596. if (column >= this.ColumnCount() || column < 0) {
  597. throw new ArrayIndexOutOfBoundsException("ArgumentInvalidColumn:" + column);
  598. }
  599.  
  600. _matrix[(row * this.ColumnCount()) + column] = value;
  601. }
  602.  
  603. /**
  604. * Returns the matrix as a one dimension generic list.
  605. * @return
  606. */
  607. public ArrayList ToList() {
  608. _matrixList = new ArrayList();
  609. for (int i = 0; i < _matrix.length; i++) {
  610. _matrixList.add(_matrix[i]);
  611. }
  612. return _matrixList;
  613. }
  614.  
  615. /**
  616. * Returns the matrix as a one dimension array. Returns Row 1 followed by Row 2 etc.
  617. * @return
  618. */
  619. public double[] ToArray() {
  620. return _matrix;
  621. }
  622.  
  623. /**
  624. * Returns a BiPolar copy of the Matrix as a new Matrix. Matrix returned will contain values or only 1 and -1 and will relflect the values of instance of the Matrix as follows:
  625. * Values greater than 0 will be represented by -1, values less than or equal to 0 will be represented by values of -1.
  626. * @return
  627. */
  628. public Matrix ToBiPolar() {
  629. Matrix result = new Matrix(this.RowCount(), this.ColumnCount());
  630. double[] biPolarMatrix = new double[_matrix.length];
  631.  
  632. for (int index = 0; index < biPolarMatrix.length; index++) {
  633. if (_matrix[index] > 0) {
  634. biPolarMatrix[index] = 1;
  635. } else {
  636. biPolarMatrix[index] = -1;
  637. }
  638. }
  639.  
  640. result._matrix = biPolarMatrix;
  641.  
  642. return result;
  643. }
  644.  
  645. /**
  646. * Returns a Binary copy of the Matrix as a new Matrix. Matrix returned will contain values or only 1 and 0 and will relflect the values of this instance of the Matrix as follows:
  647. * Values greater than 0 will be represented by 1, values less than or eqaul to 0 will be represented by values of 0.
  648. * @return
  649. */
  650. public Matrix ToBinary() {
  651. Matrix result = new Matrix(this.RowCount(), this.ColumnCount());
  652. double[] binaryMatrix = new double[_matrix.length];
  653.  
  654. for (int index = 0; index < binaryMatrix.length; index++) {
  655. if (_matrix[index] > 0) {
  656. binaryMatrix[index] = 1;
  657. } else {
  658. binaryMatrix[index] = 0;
  659. }
  660. }
  661.  
  662. result._matrix = binaryMatrix;
  663.  
  664. return result;
  665. }
  666.  
  667.  
  668. /**
  669. * Returns the matrix as a comma delimited string. Uses the default format "%.2f" and the default row seperator CrLf.
  670. * @return
  671. */
  672. public String ToString() {
  673. return this.ToString("%.2f", ",", ";");
  674. }
  675. /// <summary>
  676. /// Returns the matrix with format applied to the numeric values. Uses the default format \"F4\" and the default row seperator CrLf.
  677. /// </summary>
  678. /// <returns></returns>
  679.  
  680. /**
  681. * Returns the matrix with format applied to the numeric values.
  682. * @param format
  683. * @return
  684. */
  685. public String ToString(String format) {
  686. return this.ToString(format, ",", ";");
  687. }
  688.  
  689. /**
  690. * Returns the matrix with format applied to the numeric values and rowDelimiter
  691. * @param format
  692. * @param columnDelimiter
  693. * @param rowDelimiter
  694. * @return
  695. */
  696. public String ToString(String format, String columnDelimiter, String rowDelimiter) {
  697. StringBuilder toString = new StringBuilder();
  698. for (int index = 0; index < _matrix.length; index++) {
  699. toString.append(String.format(format, _matrix[index]));
  700.  
  701. if ((index + 1) % this.ColumnCount() == 0) {
  702. toString.append(rowDelimiter);
  703. } else {
  704. toString.append(columnDelimiter);
  705. }
  706. }
  707. String result = toString.toString();
  708.  
  709. //tidy up spaces at the end, any other character is left in place
  710. if (result.endsWith(" ")) {
  711. return result.trim();
  712. } else {
  713. return result;
  714. }
  715. }
  716.  
  717. /**
  718. * Returns true if the specified matrix is equal in value to this instance.
  719. * @param matrix
  720. * @return
  721. */
  722. public boolean Equals(Matrix matrix) {
  723. //compare rows columns and matrix values
  724. boolean result = true;
  725.  
  726. if (this.RowCount() == matrix.RowCount() && this.ColumnCount() == matrix.ColumnCount()) {
  727. //check lengths
  728. if (this._matrix.length == matrix._matrix.length) {
  729. //compare values
  730. for (int index = 0; index < this._matrix.length; index++) {
  731. if (this._matrix[index] != matrix._matrix[index]) {
  732. result = false;
  733. }
  734. }
  735. } else {
  736. result = false;
  737. }
  738. } else {
  739. result = false;
  740. }
  741.  
  742. return result;
  743.  
  744. }
  745.  
  746. /**
  747. * Returns a new Matrix that is an exact copy of this instance.
  748. * @return
  749. */
  750. public Matrix Clone() {
  751. int index = 0;
  752. Matrix clone = new Matrix(this.RowCount(), this.ColumnCount());
  753.  
  754. for (int row = 0; row < this.RowCount(); row++) {
  755.  
  756. for (int col = 0; col < this.ColumnCount(); col++) {
  757. index = (this.ColumnCount() * row) + col;
  758. clone._matrix[index] = this._matrix[index];
  759. }
  760. }
  761. return clone;
  762. }
  763.  
  764. /**
  765. * Sets all elements of the matrix to 0.
  766. */
  767. public void Clear() {
  768. for (int index = 0; index < _matrix.length; index++) {
  769. _matrix[index] = 0.0;
  770. }
  771. }
  772.  
  773. /**
  774. * Converts an array of booleanean values to an array of doubles in the range 0 to 1.
  775. * @param array
  776. * @return
  777. */
  778. private double[] ConvertToDoubleBool(boolean[] array) {
  779. double[] result = new double[array.length];
  780.  
  781. for (int index = 0; index < array.length; index++) {
  782. if (array[index]) {
  783. result[index] = 1.0;
  784. } else {
  785. result[index] = 0;
  786. }
  787. }
  788. return result;
  789. }
  790.  
  791. /**
  792. * Converts an array of booleanean values to an array of doubles in the range -1 to 1.
  793. * @param array
  794. * @return
  795. */
  796. private double[] ConvertToDoubleBiPolar(boolean[] array) {
  797. double[] result = new double[array.length];
  798.  
  799. for (int index = 0; index < array.length; index++) {
  800. if (array[index]) {
  801. result[index] = 1.0;
  802. } else {
  803. result[index] = -1.0;
  804. }
  805. }
  806. return result;
  807. }
  808.  
  809. /**
  810. * Multiplies the specified matrix by the scalar value.
  811. * @param matrix
  812. * @param scalarValue
  813. * @return
  814. */
  815. public static Matrix MultiplyScalar(Matrix matrix, double scalarValue) {
  816.  
  817. int index = 0;
  818. Matrix result = new Matrix(matrix.RowCount(), matrix.ColumnCount());
  819.  
  820. for (int row = 0; row < matrix.RowCount(); row++) {
  821.  
  822. for (int col = 0; col < matrix.ColumnCount(); col++) {
  823. index = (matrix.ColumnCount() * row) + col;
  824. result._matrix[index] = matrix._matrix[index] * scalarValue;
  825. }
  826. }
  827.  
  828. return result;
  829. }
  830.  
  831. /**
  832. * Divides the specified matrix by the scalar value.
  833. * @param matrix
  834. * @param scalarValue
  835. * @return
  836. */
  837. public static Matrix DivideScalar(Matrix matrix, double scalarValue) {
  838.  
  839. int index = 0;
  840. Matrix result = new Matrix(matrix.RowCount(), matrix.ColumnCount());
  841.  
  842. for (int row = 0; row < matrix.RowCount(); row++) {
  843.  
  844. for (int col = 0; col < matrix.ColumnCount(); col++) {
  845. index = (matrix.ColumnCount() * row) + col;
  846. result._matrix[index] = matrix._matrix[index] / scalarValue;
  847. }
  848. }
  849.  
  850. return result;
  851. }
  852.  
  853. /**
  854. * Adds the the scalar value to the specified matrix.
  855. * @param matrix
  856. * @param scalarValue
  857. * @return
  858. */
  859.  
  860. public static Matrix AddScalar(Matrix matrix, double scalarValue) {
  861.  
  862. int index = 0;
  863. Matrix result = new Matrix(matrix.RowCount(), matrix.ColumnCount());
  864.  
  865. for (int row = 0; row < matrix.RowCount(); row++) {
  866.  
  867. for (int col = 0; col < matrix.ColumnCount(); col++) {
  868. index = (matrix.ColumnCount() * row) + col;
  869. result._matrix[index] = matrix._matrix[index] + scalarValue;
  870. }
  871. }
  872.  
  873. return result;
  874. }
  875.  
  876. /**
  877. * Subtracts the the scalar value to the specified matrix.
  878. * @param matrix
  879. * @param scalarValue
  880. * @return
  881. */
  882. public static Matrix SubtractScalar(Matrix matrix, double scalarValue) {
  883.  
  884. int index = 0;
  885. Matrix result = new Matrix(matrix.RowCount(), matrix.ColumnCount());
  886.  
  887. for (int row = 0; row < matrix.RowCount(); row++) {
  888.  
  889. for (int col = 0; col < matrix.ColumnCount(); col++) {
  890. index = (matrix.ColumnCount() * row) + col;
  891. result._matrix[index] = matrix._matrix[index] - scalarValue;
  892. }
  893. }
  894.  
  895. return result;
  896. }
  897.  
  898. /**
  899. * Adds two matrices and returns the resultant matrix.
  900. * @param matrix1
  901. * @param matrix2
  902. * @return
  903. */
  904. public static Matrix Add(Matrix matrix1, Matrix matrix2) {
  905.  
  906. if (matrix1.RowCount() != matrix2.RowCount() || matrix1.ColumnCount() != matrix2.ColumnCount()) {
  907. throw new IllegalArgumentException("ArgumentMatricesIncompatible");
  908. }
  909.  
  910. int index = 0;
  911. Matrix result = new Matrix(matrix1.RowCount(), matrix1.ColumnCount());
  912.  
  913. for (int row = 0; row < matrix1.RowCount(); row++) {
  914.  
  915. for (int col = 0; col < matrix1.ColumnCount(); col++) {
  916. index = (matrix1.ColumnCount() * row) + col;
  917. result._matrix[index] = matrix1._matrix[index] + matrix2._matrix[index];
  918. }
  919. }
  920.  
  921. return result;
  922. }
  923.  
  924. /**
  925. * Subtracts matrix m2 from matrix m1 and returns the result.
  926. * @param matrix1
  927. * @param matrix2
  928. * @return
  929. */
  930. public static Matrix Subtract(Matrix matrix1, Matrix matrix2) {
  931.  
  932. if (matrix1.RowCount() != matrix2.RowCount() || matrix1.ColumnCount() != matrix2.ColumnCount()) {
  933. throw new IllegalArgumentException("ArgumentMatricesIncompatible");
  934. }
  935.  
  936. int index = 0;
  937. Matrix result = new Matrix(matrix1.RowCount(), matrix1.ColumnCount());
  938.  
  939. for (int row = 0; row < matrix1.RowCount(); row++) {
  940.  
  941. for (int col = 0; col < matrix1.ColumnCount(); col++) {
  942. index = (matrix1.ColumnCount() * row) + col;
  943. result._matrix[index] = matrix1._matrix[index] - matrix2._matrix[index];
  944. }
  945. }
  946.  
  947. return result;
  948. }
  949.  
  950. /**
  951. * Returns the Dot Product of two vectors. The respective elements of each vector are multiplied together and summed.
  952. * Note that the vectors must be the same length.
  953. * @param matrix1
  954. * @param matrix2
  955. * @return
  956. */
  957. public static double DotProduct(Matrix matrix1, Matrix matrix2) {
  958. //both must be a vector
  959. if (!(matrix1.IsVector() && matrix2.IsVector())) {
  960. throw new IllegalArgumentException("ArgumentMatricesIncompatible");
  961. }
  962. if (matrix1.length() != matrix2.length()) {
  963. throw new IllegalArgumentException("ArgumentMatricesIncompatible");
  964. }
  965.  
  966. double result = 0.0;
  967. for (int index = 0; index < matrix1.length(); index++) {
  968. result += matrix1._matrix[index] * matrix2._matrix[index];
  969. }
  970.  
  971. return result;
  972. }
  973.  
  974. /**
  975. * Multiplies the two specified matrices. Note that matrix1 must have the same number of columns as matrix2 has rows.
  976. * @param matrix1
  977. * @param matrix2
  978. * @return
  979. */
  980. public static Matrix Multiply(Matrix matrix1, Matrix matrix2) {
  981. if (matrix1.ColumnCount() != matrix2.RowCount()) {
  982. throw new IllegalArgumentException("ArgumentMatricesIncompatible");
  983. }
  984.  
  985. Matrix result = new Matrix(matrix1.RowCount(), matrix2.ColumnCount());
  986.  
  987. for (int rowIndex = 0; rowIndex < result.RowCount(); rowIndex++) {
  988.  
  989. for (int colIndex = 0; colIndex < result.ColumnCount(); colIndex++) {
  990. double[] row = matrix1.GetRow(rowIndex).ToArray();
  991. double[] col = matrix2.GetColumn(colIndex).ToArray();
  992.  
  993. //loop here to perform the multiplication
  994. for (int index = 0; index < row.length; index++) {
  995. result._matrix[(rowIndex * result.ColumnCount()) + colIndex] += row[index] * col[index];
  996. }
  997. }
  998. }
  999. return result;
  1000. }
  1001.  
  1002. /**
  1003. * Method to transpose the matrix. All rows become columns and all columns become rows.
  1004. * @param matrix
  1005. * @return
  1006. */
  1007. public static Matrix Transpose(Matrix matrix) {
  1008. //create a result matrix that is the reverse of the passed in matrix
  1009. Matrix result = new Matrix(matrix.ColumnCount(), matrix.RowCount());
  1010.  
  1011. for (int colIndex = 0; colIndex < matrix.ColumnCount(); colIndex++) {
  1012. System.arraycopy(matrix.GetColumn(colIndex).ToArray(), 0, result._matrix, colIndex * matrix.RowCount(), matrix.RowCount());
  1013. }
  1014. return result;
  1015. }
  1016. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement