001// --- BEGIN LICENSE BLOCK --- 002/* 003 * Copyright (c) 2009, Mikio L. Braun 004 * All rights reserved. 005 * 006 * Redistribution and use in source and binary forms, with or without 007 * modification, are permitted provided that the following conditions are 008 * met: 009 * 010 * * Redistributions of source code must retain the above copyright 011 * notice, this list of conditions and the following disclaimer. 012 * 013 * * Redistributions in binary form must reproduce the above 014 * copyright notice, this list of conditions and the following 015 * disclaimer in the documentation and/or other materials provided 016 * with the distribution. 017 * 018 * * Neither the name of the Technische Universität Berlin nor the 019 * names of its contributors may be used to endorse or promote 020 * products derived from this software without specific prior 021 * written permission. 022 * 023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 024 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 025 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 026 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 027 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 028 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 029 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 030 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 031 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 032 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 034 */ 035// --- END LICENSE BLOCK --- 036 037package org.jblas; 038 039import org.jblas.exceptions.SizeException; 040 041import java.io.DataInputStream; 042import java.io.DataOutputStream; 043import java.io.FileInputStream; 044import java.io.FileOutputStream; 045import java.io.IOException; 046import java.util.Arrays; 047 048public class ComplexDoubleMatrix { 049 050 public int rows; 051 public int columns; 052 public int length; 053 public double[] data = null; // rows are contiguous 054 055 /************************************************************************** 056 * 057 * Constructors and factory functions 058 * 059 **************************************************************************/ 060 061 /** 062 * Create a new matrix with <i>newRows</i> rows, <i>newColumns</i> columns 063 * using <i>newData></i> as the data. 064 */ 065 public ComplexDoubleMatrix(int newRows, int newColumns, double... newData) { 066 rows = newRows; 067 columns = newColumns; 068 length = rows * columns; 069 070 if (newData.length != 2 * newRows * newColumns) 071 throw new IllegalArgumentException( 072 "Passed data must match matrix dimensions."); 073 data = newData; 074 } 075 076 /** 077 * Creates a new <i>n</i> times <i>m</i> <tt>ComplexDoubleMatrix</tt>. 078 * @param newRows the number of rows (<i>n</i>) of the new matrix. 079 * @param newColumns the number of columns (<i>m</i>) of the new matrix. 080 */ 081 public ComplexDoubleMatrix(int newRows, int newColumns) { 082 this(newRows, newColumns, new double[2 * newRows * newColumns]); 083 } 084 085 /** 086 * Creates a new <tt>ComplexDoubleMatrix</tt> of size 0 times 0. 087 */ 088 public ComplexDoubleMatrix() { 089 this(0, 0, null); 090 } 091 092 /** 093 * Create a Matrix of length <tt>len</tt>. By default, this creates a row vector. 094 * @param len 095 */ 096 public ComplexDoubleMatrix(int len) { 097 this(len, 1, new double[2 * len]); 098 } 099 100 public ComplexDoubleMatrix(double[] newData) { 101 this(newData.length/2, 1, newData); 102 } 103 104 public ComplexDoubleMatrix(ComplexDouble[] newData) { 105 this(newData.length); 106 107 for (int i = 0; i < newData.length; i++) 108 put(i, newData[i]); 109 } 110 111 112 /** Construct a complex matrix from a real matrix. */ 113 public ComplexDoubleMatrix(DoubleMatrix m) { 114 this(m.rows, m.columns); 115 116 NativeBlas.dcopy(m.length, m.data, 0, 1, data, 0, 2); 117 } 118 119 /** Construct a complex matrix from separate real and imaginary parts. Either 120 * part can be set to null in which case it will be ignored. 121 */ 122 public ComplexDoubleMatrix(DoubleMatrix real, DoubleMatrix imag) { 123 this(real.rows, real.columns); 124 real.assertSameSize(imag); 125 126 if (real != null) 127 NativeBlas.dcopy(length, real.data, 0, 1, data, 0, 2); 128 if (imag != null) 129 NativeBlas.dcopy(length, imag.data, 0, 1, data, 1, 2); 130 } 131 132 /** 133 * Creates a new matrix by reading it from a file. 134 * @param filename the path and name of the file to read the matrix from 135 * @throws IOException 136 */ 137 public ComplexDoubleMatrix(String filename) throws IOException { 138 load(filename); 139 } 140 141 /** 142 * Creates a new <i>n</i> times <i>m</i> <tt>ComplexDoubleMatrix</tt> from 143 * the given <i>n</i> times <i>m</i> 2D data array. The first dimension of the array makes the 144 * rows (<i>n</i>) and the second dimension the columns (<i>m</i>). For example, the 145 * given code <br/><br/> 146 * <code>new ComplexDoubleMatrix(new double[][]{{1d, 2d, 3d}, {4d, 5d, 6d}, {7d, 8d, 9d}}).print();</code><br/><br/> 147 * will constructs the following matrix: 148 * <pre> 149 * 1.0 2.0 3.0 150 * 4.0 5.0 6.0 151 * 7.0 8.0 9.0 152 * </pre>. 153 * @param data <i>n</i> times <i>m</i> data array 154 */ 155 public ComplexDoubleMatrix(double[][] data) { 156 this(data.length, data[0].length); 157 158 for (int r = 0; r < rows; r++) 159 assert(data[r].length == columns); 160 161 for (int r = 0; r < rows; r++) 162 for (int c = 0; c < columns; c++) 163 put(r, c, data[r][c]); 164 } 165 166 /** 167 * Creates a new matrix in which all values are equal 0. 168 * @param rows number of rows 169 * @param columns number of columns 170 * @return new matrix 171 */ 172 public static ComplexDoubleMatrix zeros(int rows, int columns) { 173 return new ComplexDoubleMatrix(rows, columns); 174 } 175 176 public static ComplexDoubleMatrix zeros(int length) { 177 return zeros(length, 1); 178 } 179 180 /** 181 * Creates a new matrix in which all values are equal 1. 182 * @param rows number of rows 183 * @param columns number of columns 184 * @return new matrix 185 */ 186 public static ComplexDoubleMatrix ones(int rows, int columns) { 187 ComplexDoubleMatrix m = new ComplexDoubleMatrix(rows, columns); 188 189 for (int i = 0; i < rows * columns; i++) 190 m.put(i, 1.0); 191 192 return m; 193 } 194 195 public static ComplexDoubleMatrix ones(int length) { 196 return ones(length, 1); 197 } 198 199 /** 200 * Creates a new matrix where the values of the given vector are the diagonal values of 201 * the matrix. 202 * @param x the diagonal values 203 * @return new matrix 204 */ 205 public static ComplexDoubleMatrix diag(ComplexDoubleMatrix x) { 206 ComplexDoubleMatrix m = new ComplexDoubleMatrix(x.length, x.length); 207 208 for (int i = 0; i < x.length; i++) 209 m.put(i, i, x.get(i)); 210 211 return m; 212 } 213 214 /** 215 * Construct a matrix of arbitrary shape and set the diagonal according 216 * to a passed vector. 217 * 218 * length of needs to be smaller than rows or columns. 219 * 220 * @param x vector to fill the diagonal with 221 * @param rows number of rows of the resulting matrix 222 * @param columns number of columns of the resulting matrix 223 * @return a matrix with dimensions rows * columns whose diagonal elements are filled by x 224 */ 225 public static ComplexDoubleMatrix diag(ComplexDoubleMatrix x, int rows, int columns) { 226 if (x.length > rows || x.length > columns) { 227 throw new SizeException("Length of diagonal matrix must be larger than both rows and columns."); 228 } 229 230 ComplexDoubleMatrix m = new ComplexDoubleMatrix(rows, columns); 231 232 for (int i = 0; i < x.length; i++) 233 m.put(i, i, x.get(i)); 234 235 return m; 236 } 237 238 /** 239 * Create a 1 * 1 - matrix. For many operations, this matrix functions like a 240 * normal double 241 * @param s value of the matrix 242 * @return the constructed ComplexDoubleMatrix 243 */ 244 public static ComplexDoubleMatrix scalar(double s) { 245 ComplexDoubleMatrix m = new ComplexDoubleMatrix(1, 1); 246 m.put(0, 0, s); 247 return m; 248 } 249 250 /** Test whether a matrix is scalar */ 251 public boolean isScalar() { 252 return length == 1; 253 } 254 255 /** Return the first element of the matrix */ 256 public ComplexDouble scalar() { 257 return get(0); 258 } 259 260 public static ComplexDoubleMatrix concatHorizontally(ComplexDoubleMatrix A, ComplexDoubleMatrix B) { 261 if (A.rows != B.rows) 262 throw new SizeException("Matrices don't have same number of rows."); 263 264 ComplexDoubleMatrix result = new ComplexDoubleMatrix(A.rows, A.columns + B.columns); 265 SimpleBlas.copy(A, result); 266 NativeBlas.zcopy(B.length, B.data, 0, 1, result.data, A.length, 1); 267 return result; 268 } 269 270 public static ComplexDoubleMatrix concatVertically(ComplexDoubleMatrix A, ComplexDoubleMatrix B) { 271 if (A.columns != B.columns) 272 throw new SizeException("Matrices don't have same number of columns."); 273 274 ComplexDoubleMatrix result = new ComplexDoubleMatrix(A.rows + B.rows, A.columns); 275 276 for (int i = 0; i < A.columns; i++) { 277 NativeBlas.zcopy(A.rows, A.data, A.index(0, i), 1, result.data, result.index(0, i), 1); 278 NativeBlas.zcopy(B.rows, B.data, B.index(0, i), 1, result.data, result.index(A.rows, i), 1); 279 } 280 281 return result; 282 } 283 284 /************************************************************************** 285 * Working with slices (Man! 30+ methods just to make this a bit flexible...) 286 */ 287 288 public ComplexDoubleMatrix get(int[] indices) { 289 ComplexDoubleMatrix result = new ComplexDoubleMatrix(indices.length); 290 291 for (int i = 0; i < indices.length; i++) 292 result.put(i, get(indices[i])); 293 294 return result; 295 } 296 297 public ComplexDoubleMatrix get(int r, int[] indices) { 298 ComplexDoubleMatrix result = new ComplexDoubleMatrix(1, indices.length); 299 300 for (int i = 0; i < indices.length; i++) 301 result.put(i, get(r, indices[i])); 302 303 return result; 304 } 305 306 public ComplexDoubleMatrix get(int[] indices, int c) { 307 ComplexDoubleMatrix result = new ComplexDoubleMatrix(indices.length, 1); 308 309 for (int i = 0; i < indices.length; i++) 310 result.put(i, get(indices[i], c)); 311 312 return result; 313 } 314 315 public ComplexDoubleMatrix get(int[] rindices, int[] cindices) { 316 ComplexDoubleMatrix result = new ComplexDoubleMatrix(rindices.length, cindices.length); 317 318 for (int i = 0; i < rindices.length; i++) 319 for (int j = 0; j < cindices.length; j++) 320 result.put(i, j, get(rindices[i], cindices[j])); 321 322 return result; 323 } 324 325 public ComplexDoubleMatrix get(ComplexDoubleMatrix indices) { 326 return get(indices.findIndices()); 327 } 328 329 public ComplexDoubleMatrix get(int r, ComplexDoubleMatrix indices) { 330 return get(r, indices.findIndices()); 331 } 332 333 public ComplexDoubleMatrix get(ComplexDoubleMatrix indices, int c) { 334 return get(indices.findIndices(), c); 335 } 336 337 public ComplexDoubleMatrix get(ComplexDoubleMatrix rindices, ComplexDoubleMatrix cindices) { 338 return get(rindices.findIndices(), cindices.findIndices()); 339 } 340 341 private void checkLength(int l) { 342 if (length != l) 343 throw new SizeException("Matrix does not have the necessary length (" + length + " != " + l + ")."); 344 } 345 346 private void checkRows(int r) { 347 if (rows != r) 348 throw new SizeException("Matrix does not have the necessary length (" + length + " != " + r + ")."); 349 } 350 351 private void checkColumns(int c) { 352 if (columns != c) 353 throw new SizeException("Matrix does not have the necessary length (" + length + " != " + c + ")."); 354 } 355 356 public ComplexDoubleMatrix put(int[] indices, ComplexDoubleMatrix x) { 357 if (x.isScalar()) 358 return put(indices, x.scalar()); 359 x.checkLength(indices.length); 360 361 for (int i = 0; i < indices.length; i++) 362 put(indices[i], x.get(i)); 363 364 return this; 365 } 366 367 public ComplexDoubleMatrix put(int r, int[] indices, ComplexDoubleMatrix x) { 368 if (x.isScalar()) 369 return put(r, indices, x.scalar()); 370 x.checkColumns(indices.length); 371 372 for (int i = 0; i < indices.length; i++) 373 put(r, indices[i], x.get(i)); 374 375 return this; 376 } 377 378 public ComplexDoubleMatrix put(int[] indices, int c, ComplexDoubleMatrix x) { 379 if (x.isScalar()) 380 return put(indices, c, x.scalar()); 381 x.checkRows(indices.length); 382 383 for (int i = 0; i < indices.length; i++) 384 put(indices[i], c, x.get(i)); 385 386 return this; 387 } 388 389 public ComplexDoubleMatrix put(int[] rindices, int[] cindices, ComplexDoubleMatrix x) { 390 if (x.isScalar()) 391 return put(rindices, cindices, x.scalar()); 392 x.checkRows(rindices.length); 393 x.checkColumns(cindices.length); 394 395 for (int i = 0; i < rindices.length; i++) 396 for (int j = 0; j < cindices.length; j++) 397 put(rindices[i], cindices[j], x.get(i,j)); 398 399 return this; 400 } 401 402 public ComplexDoubleMatrix put(int[] indices, double v) { 403 for (int i = 0; i < indices.length; i++) 404 put(indices[i], v); 405 406 return this; 407 } 408 409 public ComplexDoubleMatrix putReal(int[] indices, double v) { 410 return put(indices, v); 411 } 412 413 public ComplexDoubleMatrix putImag(int[] indices, double v) { 414 for (int i = 0; i < indices.length; i++) 415 putImag(indices[i], v); 416 417 return this; 418 } 419 420 public ComplexDoubleMatrix put(int[] indices, ComplexDouble v) { 421 for (int i = 0; i < indices.length; i++) 422 put(indices[i], v); 423 424 return this; 425 } 426 427 public ComplexDoubleMatrix put(int r, int[] indices, double v) { 428 for (int i = 0; i < indices.length; i++) 429 put(r, indices[i], v); 430 431 return this; 432 } 433 434 public ComplexDoubleMatrix putReal(int r, int[] indices, double v) { 435 return put(r, indices, v); 436 } 437 438 public ComplexDoubleMatrix putImag(int r, int[] indices, double v) { 439 for (int i = 0; i < indices.length; i++) 440 putImag(r, indices[i], v); 441 442 return this; 443 } 444 445 public ComplexDoubleMatrix put(int r, int[] indices, ComplexDouble v) { 446 for (int i = 0; i < indices.length; i++) 447 put(r, indices[i], v); 448 449 return this; 450 } 451 452 public ComplexDoubleMatrix put(int[] indices, int c, double v) { 453 for (int i = 0; i < indices.length; i++) 454 put(indices[i], c, v); 455 456 return this; 457 } 458 459 public ComplexDoubleMatrix putReal(int[] indices, int c, double v) { 460 return put(indices, c, v); 461 } 462 463 public ComplexDoubleMatrix putImag(int[] indices, int c, double v) { 464 for (int i = 0; i < indices.length; i++) 465 putImag(indices[i], c, v); 466 467 return this; 468 } 469 470 public ComplexDoubleMatrix put(int[] indices, int c, ComplexDouble v) { 471 for (int i = 0; i < indices.length; i++) 472 put(indices[i], c, v); 473 474 return this; 475 } 476 477 public ComplexDoubleMatrix put(int[] rindices, int[] cindices, double v) { 478 for (int i = 0; i < rindices.length; i++) 479 for (int j = 0; j < cindices.length; j++) 480 put(rindices[i], cindices[j], v); 481 482 return this; 483 } 484 485 public ComplexDoubleMatrix putReal(int[] rindices, int[] cindices, double v) { 486 return put(rindices, cindices, v); 487 } 488 489 public ComplexDoubleMatrix putImag(int[] rindices, int[] cindices, double v) { 490 for (int i = 0; i < rindices.length; i++) 491 for (int j = 0; j < cindices.length; j++) 492 put(rindices[i], cindices[j], v); 493 494 return this; 495 } 496 497 public ComplexDoubleMatrix put(int[] rindices, int[] cindices, ComplexDouble v) { 498 for (int i = 0; i < rindices.length; i++) 499 for (int j = 0; j < cindices.length; j++) 500 put(rindices[i], cindices[j], v); 501 502 return this; 503 } 504 505 public ComplexDoubleMatrix put(ComplexDoubleMatrix indices, ComplexDoubleMatrix v) { 506 return put(indices.findIndices(), v); 507 } 508 509 public ComplexDoubleMatrix put(int r, ComplexDoubleMatrix indices, ComplexDoubleMatrix v) { 510 return put(r, indices.findIndices(), v); 511 } 512 513 public ComplexDoubleMatrix put(ComplexDoubleMatrix indices, int c, ComplexDoubleMatrix v) { 514 return put(indices.findIndices(), c, v); 515 } 516 517 public ComplexDoubleMatrix put(ComplexDoubleMatrix rindices, ComplexDoubleMatrix cindices, ComplexDoubleMatrix v) { 518 return put(rindices.findIndices(), cindices.findIndices(), v); 519 } 520 521 public ComplexDoubleMatrix put(ComplexDoubleMatrix indices, double v) { 522 return put(indices.findIndices(), v); 523 } 524 525 public ComplexDoubleMatrix putReal(ComplexDoubleMatrix indices, double v) { 526 return put(indices, v); 527 } 528 529 public ComplexDoubleMatrix putImag(ComplexDoubleMatrix indices, double v) { 530 return putImag(indices.findIndices(), v); 531 } 532 533 public ComplexDoubleMatrix put(ComplexDoubleMatrix indices, ComplexDouble v) { 534 return put(indices.findIndices(), v); 535 } 536 537 public ComplexDoubleMatrix put(int r, ComplexDoubleMatrix indices, double v) { 538 return put(r, indices.findIndices(), v); 539 } 540 541 public ComplexDoubleMatrix putReal(int r, ComplexDoubleMatrix indices, double v) { 542 return put(r, indices, v); 543 } 544 545 public ComplexDoubleMatrix putImag(int r, ComplexDoubleMatrix indices, double v) { 546 return putImag(r, indices.findIndices(), v); 547 } 548 549 public ComplexDoubleMatrix put(int r, ComplexDoubleMatrix indices, ComplexDouble v) { 550 return put(r, indices.findIndices(), v); 551 } 552 553 public ComplexDoubleMatrix put(ComplexDoubleMatrix indices, int c, double v) { 554 return put(indices.findIndices(), c, v); 555 } 556 557 public ComplexDoubleMatrix putReal(ComplexDoubleMatrix indices, int c, double v) { 558 return put(indices, c, v); 559 } 560 561 public ComplexDoubleMatrix putImag(ComplexDoubleMatrix indices, int c, double v) { 562 return putImag(indices.findIndices(), c, v); 563 } 564 565 public ComplexDoubleMatrix put(ComplexDoubleMatrix indices, int c, ComplexDouble v) { 566 return put(indices.findIndices(), c, v); 567 } 568 569 public ComplexDoubleMatrix put(ComplexDoubleMatrix rindices, ComplexDoubleMatrix cindices, double v) { 570 return put(rindices.findIndices(), cindices.findIndices(), v); 571 } 572 573 public ComplexDoubleMatrix putReal(ComplexDoubleMatrix rindices, ComplexDoubleMatrix cindices, double v) { 574 return putReal(rindices.findIndices(), cindices.findIndices(), v); 575 } 576 577 public ComplexDoubleMatrix putImag(ComplexDoubleMatrix rindices, ComplexDoubleMatrix cindices, double v) { 578 return putImag(rindices.findIndices(), cindices.findIndices(), v); 579 } 580 581 public ComplexDoubleMatrix put(ComplexDoubleMatrix rindices, ComplexDoubleMatrix cindices, ComplexDouble v) { 582 return put(rindices.findIndices(), cindices.findIndices(), v); 583 } 584 585 586 public int[] findIndices() { 587 int len = 0; 588 for (int i = 0; i < length; i++) 589 if (!get(i).isZero()) 590 len++; 591 592 int[] indices = new int[len]; 593 int c = 0; 594 595 for (int i = 0; i < length; i++) 596 if (!get(i).isZero()) 597 indices[c++] = i; 598 599 return indices; 600 } 601 602 /************************************************************************** 603 * Basic operations (copying, resizing, element access) 604 */ 605 606 /** Return transposed copy of this matrix */ 607 public ComplexDoubleMatrix transpose() { 608 ComplexDoubleMatrix result = new ComplexDoubleMatrix(columns, rows); 609 610 ComplexDouble c = new ComplexDouble(0); 611 612 for (int i = 0; i < rows; i++) 613 for (int j = 0; j < columns; j++) 614 result.put(j, i, get(i, j, c)); 615 616 return result; 617 } 618 619 public ComplexDoubleMatrix hermitian() { 620 ComplexDoubleMatrix result = new ComplexDoubleMatrix(columns, rows); 621 622 ComplexDouble c = new ComplexDouble(0); 623 624 for (int i = 0; i < rows; i++) 625 for (int j = 0; j < columns; j++) 626 result.put(j, i, get(i, j, c).conji()); 627 return result; 628 } 629 630 /** 631 * Compute complex conjugate (in-place). 632 */ 633 public ComplexDoubleMatrix conji() { 634 ComplexDouble c = new ComplexDouble(0.0); 635 for (int i = 0; i < length; i++) 636 put(i, get(i, c).conji()); 637 return this; 638 } 639 640 /** 641 * Compute complex conjugate. 642 */ 643 public ComplexDoubleMatrix conj() { 644 return dup().conji(); 645 } 646 647 648 /** Compare two matrices. 649 * @param o Object to compare to 650 * @return true if and only if other is also a ComplexDoubleMatrix which has the same size and the 651 * maximal absolute difference in matrix elements is smaller thatn 1e-6. */ 652 public boolean equals(Object o) { 653 if (!(o instanceof ComplexDoubleMatrix)) 654 return false; 655 656 ComplexDoubleMatrix other = (ComplexDoubleMatrix) o; 657 658 if (!sameSize(other)) 659 return false; 660 661 return Arrays.equals(data, other.data); 662 } 663 664 public int hashCode() { 665 return rows ^ columns ^ Arrays.hashCode(data); 666 } 667 668 669 /** Resize the matrix. All elements will be set to zero. */ 670 public void resize(int newRows, int newColumns) { 671 rows = newRows; 672 columns = newColumns; 673 length = newRows * newColumns; 674 data = new double[2 * rows * columns]; 675 } 676 677 678 /** Reshape the matrix. Number of elements must not change. */ 679 public ComplexDoubleMatrix reshape(int newRows, int newColumns) { 680 if (length != newRows * newColumns) 681 throw new IllegalArgumentException( 682 "Number of elements must not change."); 683 684 rows = newRows; 685 columns = newColumns; 686 687 return this; 688 } 689 690 /** Checks whether two matrices have the same size. */ 691 public boolean sameSize(ComplexDoubleMatrix a) { 692 return rows == a.rows && columns == a.columns; 693 } 694 695 /** 696 * Assert that two matrices have the same size. 697 * 698 * @param a the other matrix 699 * @throws SizeException if matrix sizes don't match. 700 * */ 701 public void assertSameSize(ComplexDoubleMatrix a) { 702 if (!sameSize(a)) 703 throw new SizeException("Matrices must have the same size."); 704 } 705 706 /** 707 * Check whether this can be multiplied with a. 708 * 709 * @param a right-hand-side of the multiplication. 710 * @return true iff <tt>this.columns == a.rows</tt> 711 */ 712 public boolean multipliesWith(ComplexDoubleMatrix a) { 713 return columns == a.rows; 714 } 715 716 public void assertMultipliesWith(ComplexDoubleMatrix a) { 717 if (!multipliesWith(a)) 718 throw new SizeException("Number of columns of left matrix must be equal to number of rows of right matrix."); 719 } 720 721 public boolean sameLength(ComplexDoubleMatrix a) { 722 return length == a.length; 723 } 724 725 public void assertSameLength(ComplexDoubleMatrix a) { 726 if (!sameLength(a)) 727 throw new SizeException("Matrices must have same length (is: " + length + " and " + a.length + ")"); 728 } 729 730 /** Copy ComplexDoubleMatrix a to this. this a is resized if necessary. */ 731 public ComplexDoubleMatrix copy(ComplexDoubleMatrix a) { 732 if (!sameSize(a)) 733 resize(a.rows, a.columns); 734 735 SimpleBlas.copy(a, this); 736 return a; 737 } 738 739 /** Returns a duplicate of this matrix. Geometry is the same (including offsets, transpose, etc.), 740 * but the buffer is not shared. 741 */ 742 public ComplexDoubleMatrix dup() { 743 ComplexDoubleMatrix out = new ComplexDoubleMatrix(rows, columns); 744 745 JavaBlas.rcopy(2*length, data, 0, 1, out.data, 0, 1); 746 747 return out; 748 } 749 750 public ComplexDoubleMatrix swapColumns(int i, int j) { 751 NativeBlas.zswap(rows, data, index(0, i), 1, data, index(0, j), 1); 752 return this; 753 } 754 755 public ComplexDoubleMatrix swapRows(int i, int j) { 756 NativeBlas.zswap(columns, data, index(i, 0), rows, data, index(j, 0), rows); 757 return this; 758 } 759 760 /** Set matrix element */ 761 public ComplexDoubleMatrix put(int rowIndex, int columnIndex, double value) { 762 data[2*index(rowIndex, columnIndex)] = value; 763 return this; 764 } 765 766 public ComplexDoubleMatrix put(int rowIndex, int columnIndex, double realValue, double complexValue) { 767 data[2*index(rowIndex, columnIndex)] = realValue; 768 data[2*index(rowIndex, columnIndex)+1] = complexValue; 769 return this; 770 } 771 772 public ComplexDoubleMatrix put(int rowIndex, int columnIndex, ComplexDouble value) { 773 int i = 2*index(rowIndex, columnIndex); 774 data[i] = value.real(); data[i+1] = value.imag(); 775 return this; 776 } 777 778 public ComplexDoubleMatrix putReal(int rowIndex, int columnIndex, double value) { 779 data[2*index(rowIndex, columnIndex)] = value; 780 return this; 781 } 782 783 public ComplexDoubleMatrix putImag(int rowIndex, int columnIndex, double value) { 784 data[2*index(rowIndex, columnIndex)+1] = value; 785 return this; 786 } 787 788 /** Retrieve matrix element */ 789 public ComplexDouble get(int rowIndex, int columnIndex) { 790 int i = 2*index(rowIndex, columnIndex); 791 return new ComplexDouble(data[i], data[i+1]); 792 } 793 794 /** Get matrix element, passing the variable to store the result. */ 795 public ComplexDouble get(int rowIndex, int columnIndex, ComplexDouble result) { 796 return get(index(rowIndex, columnIndex), result); 797 } 798 799 public DoubleMatrix getReal() { 800 DoubleMatrix result = new DoubleMatrix(rows, columns); 801 802 NativeBlas.dcopy(length, data, 0, 2, result.data, 0, 1); 803 804 return result; 805 } 806 807 /** Get index of an element */ 808 public int index(int rowIndex, int columnIndex) { 809 return rows * columnIndex + rowIndex; 810 } 811 812 /** Compute the row index of a linear index. */ 813 public int indexRows(int i) { 814 return i - indexColumns(i) * rows; 815 } 816 817 /** Compute the column index of a linear index. */ 818 public int indexColumns(int i) { 819 return i / rows; 820 } 821 822 823 public ComplexDouble get(int i) { 824 return new ComplexDouble(data[i * 2], data[i * 2 + 1]); 825 } 826 827 public ComplexDouble get(int i, ComplexDouble result) { 828 return result.set(data[i * 2], data[i*2+1]); 829 } 830 831 public double getReal(int i) { 832 return data[2*i]; 833 } 834 835 public double getImag(int i) { 836 return data[2*i + 1]; 837 } 838 839 public ComplexDoubleMatrix put(int i, double v) { 840 data[2*i] = v; 841 return this; 842 } 843 844 public ComplexDoubleMatrix put(int i, double r, double c) { 845 data[2*i] = r; 846 data[2*i+1] = c; 847 return this; 848 } 849 850 public ComplexDoubleMatrix put(int i, ComplexDouble v) { 851 data[2*i] = v.real(); 852 data[2*i+1] = v.imag(); 853 return this; 854 } 855 856 public ComplexDoubleMatrix putReal(int i, double v) { 857 return put(i, v); 858 } 859 860 public ComplexDoubleMatrix putImag(int i, double v) { 861 data[2*i+1] = v; 862 return this; 863 } 864 865 public int getRows() { 866 return rows; 867 } 868 869 public int getColumns() { 870 return columns; 871 } 872 873 public int getLength() { 874 return length; 875 } 876 877 /** Checks whether the matrix is empty. */ 878 public boolean isEmpty() { 879 return columns == 0 || rows == 0; 880 } 881 882 /** Checks whether the matrix is square. */ 883 public boolean isSquare() { 884 return columns == rows; 885 } 886 887 public void assertSquare() { 888 if (!isSquare()) 889 throw new SizeException("Matrix must be square!"); 890 } 891 892 /** Checks whether the matrix is a vector. */ 893 public boolean isVector() { 894 return columns == 1 || rows == 1; 895 } 896 897 public boolean isRowVector() { 898 return columns == 1; 899 } 900 901 public boolean isColumnVector() { 902 return rows == 1; 903 } 904 905 /** Get diagonal of the matrix. */ 906 public ComplexDoubleMatrix diag() { 907 ComplexDoubleMatrix d = new ComplexDoubleMatrix(rows); 908 NativeBlas.zcopy(rows, data, 0, rows + 1, d.data, 0, 1); 909 return d; 910 } 911 912 /** Get real part of the matrix. */ 913 public DoubleMatrix real() { 914 DoubleMatrix result = new DoubleMatrix(rows, columns); 915 NativeBlas.dcopy(length, data, 0, 2, result.data, 0, 1); 916 return result; 917 } 918 919 /** Get imaginary part of the matrix. */ 920 public DoubleMatrix imag() { 921 DoubleMatrix result = new DoubleMatrix(rows, columns); 922 NativeBlas.dcopy(length, data, 1, 2, result.data, 0, 1); 923 return result; 924 } 925 926 927 /** 928 * Pretty-print this matrix to <tt>System.out</tt>. 929 * */ 930 public void print() { 931 System.out.println(toString()); 932 } 933 934 /** 935 * Generate string representation of this matrix 936 * (multi-line). 937 * */ 938 public String toString() { 939 StringBuilder s = new StringBuilder(); 940 941 s.append("["); 942 943 for (int i = 0; i < rows; i++) { 944 for (int j = 0; j < columns; j++) { 945 s.append(get(i, j)); 946 if (j < columns - 1) 947 s.append(", "); 948 } 949 if (i < rows - 1) 950 s.append("; "); 951 } 952 953 s.append("]"); 954 955 return s.toString(); 956 } 957 958 public double[] toDoubleArray() { 959 return data.clone(); 960 } 961 962 public ComplexDouble[] toArray() { 963 ComplexDouble[] array = new ComplexDouble[length]; 964 965 for (int i = 0; i < length; i++) 966 array[i] = get(i); 967 968 return array; 969 } 970 971 public ComplexDouble[][] toArray2() { 972 ComplexDouble[][] array = new ComplexDouble[rows][columns]; 973 974 for (int r = 0; r < rows; r++) 975 for (int c = 0; c < columns; c++) 976 array[r][c] = get(r, c); 977 978 return array; 979 } 980 981 public boolean[] toBooleanArray() { 982 boolean[] array = new boolean[length]; 983 984 for (int i = 0; i < length; i++) 985 array[i] = !get(i).isZero(); 986 987 return array; 988 } 989 990 public boolean[][] toBooleanArray2() { 991 boolean[][] array = new boolean[rows][columns]; 992 993 for (int r = 0; r < rows; r++) 994 for (int c = 0; c < columns; c++) 995 array[r][c] = !get(r, c).isZero(); 996 997 return array; 998 } 999 1000 /************************************************************************** 1001 * Arithmetic Operations 1002 */ 1003 1004 /** 1005 * Ensures that the result vector has the same length as this. If not, 1006 * resizing result is tried, which fails if result == this or result == other. 1007 */ 1008 private void ensureResultLength(ComplexDoubleMatrix other, ComplexDoubleMatrix result) { 1009 if (!sameLength(result)) { 1010 if (result == this || result == other) 1011 throw new SizeException("Cannot resize result matrix because it is used in-place."); 1012 result.resize(rows, columns); 1013 } 1014 } 1015 1016 /** Add two matrices. */ 1017 public ComplexDoubleMatrix addi(ComplexDoubleMatrix other, ComplexDoubleMatrix result) { 1018 if (other.isScalar()) 1019 return addi(other.scalar(), result); 1020 1021 assertSameLength(other); 1022 ensureResultLength(other, result); 1023 1024 if (result == this) 1025 SimpleBlas.axpy(ComplexDouble.UNIT, other, result); 1026 else if (result == other) 1027 SimpleBlas.axpy(ComplexDouble.UNIT, this, result); 1028 else { 1029 SimpleBlas.copy(this, result); 1030 SimpleBlas.axpy(ComplexDouble.UNIT, other, result); 1031 } 1032 1033 return result; 1034 } 1035 1036 /** Add a scalar to a matrix. */ 1037 public ComplexDoubleMatrix addi(ComplexDouble v, ComplexDoubleMatrix result) { 1038 ensureResultLength(null, result); 1039 1040 for (int i = 0; i < length; i++) 1041 result.put(i, get(i).add(v)); 1042 return result; 1043 } 1044 1045 public ComplexDoubleMatrix addi(double v, ComplexDoubleMatrix result) { 1046 return addi(new ComplexDouble(v), result); 1047 } 1048 1049 /** Subtract two matrices. */ 1050 public ComplexDoubleMatrix subi(ComplexDoubleMatrix other, ComplexDoubleMatrix result) { 1051 if (other.isScalar()) 1052 return subi(other.scalar(), result); 1053 1054 assertSameLength(other); 1055 ensureResultLength(other, result); 1056 1057 if (result == this) 1058 SimpleBlas.axpy(ComplexDouble.NEG_UNIT, other, result); 1059 else if (result == other) { 1060 SimpleBlas.scal(ComplexDouble.NEG_UNIT, result); 1061 SimpleBlas.axpy(ComplexDouble.UNIT, this, result); 1062 } 1063 else { 1064 SimpleBlas.copy(this, result); 1065 SimpleBlas.axpy(ComplexDouble.NEG_UNIT, other, result); 1066 } 1067 return result; 1068 } 1069 1070 /** Subtract a scalar from a matrix */ 1071 public ComplexDoubleMatrix subi(ComplexDouble v, ComplexDoubleMatrix result) { 1072 ensureResultLength(null, result); 1073 1074 for (int i = 0; i < length; i++) 1075 result.put(i, get(i).sub(v)); 1076 return result; 1077 } 1078 1079 public ComplexDoubleMatrix subi(double v, ComplexDoubleMatrix result) { 1080 return subi(new ComplexDouble(v), result); 1081 } 1082 1083 /** 1084 * Subtract two matrices, but subtract first from second matrix, that is, 1085 * compute <em>result = other - this</em>. 1086 * */ 1087 public ComplexDoubleMatrix rsubi(ComplexDoubleMatrix other, ComplexDoubleMatrix result) { 1088 return other.subi(this, result); 1089 } 1090 1091 /** Subtract a matrix from a scalar */ 1092 public ComplexDoubleMatrix rsubi(ComplexDouble a, ComplexDoubleMatrix result) { 1093 ensureResultLength(null, result); 1094 1095 for (int i = 0; i < length; i++) 1096 result.put(i, a.sub(get(i))); 1097 return result; 1098 } 1099 1100 public ComplexDoubleMatrix rsubi(double a, ComplexDoubleMatrix result) { 1101 return rsubi(new ComplexDouble(a), result); 1102 } 1103 1104 /** (Elementwise) Multiplication */ 1105 public ComplexDoubleMatrix muli(ComplexDoubleMatrix other, ComplexDoubleMatrix result) { 1106 if (other.isScalar()) 1107 return muli(other.scalar(), result); 1108 1109 assertSameLength(other); 1110 ensureResultLength(other, result); 1111 1112 ComplexDouble c = new ComplexDouble(0.0); 1113 ComplexDouble d = new ComplexDouble(0.0); 1114 1115 for (int i = 0; i < length; i++) 1116 result.put(i, get(i, c).muli(other.get(i, d))); 1117 return result; 1118 } 1119 1120 /** (Elementwise) Multiplication with a scalar */ 1121 public ComplexDoubleMatrix muli(ComplexDouble v, ComplexDoubleMatrix result) { 1122 ensureResultLength(null, result); 1123 1124 ComplexDouble c = new ComplexDouble(0.0); 1125 1126 for (int i = 0; i < length; i++) 1127 result.put(i, get(i, c).muli(v)); 1128 return result; 1129 } 1130 1131 public ComplexDoubleMatrix muli(double v, ComplexDoubleMatrix result) { 1132 return muli(new ComplexDouble(v), result); 1133 } 1134 1135 /** Matrix-Matrix Multiplication */ 1136 public ComplexDoubleMatrix mmuli(ComplexDoubleMatrix other, ComplexDoubleMatrix result) { 1137 if (other.isScalar()) 1138 return muli(other.scalar(), result); 1139 1140 /* check sizes and resize if necessary */ 1141 assertMultipliesWith(other); 1142 if (result.rows != rows || result.columns != other.columns) { 1143 if (result != this && result != other) 1144 result.resize(rows, other.columns); 1145 else 1146 throw new SizeException("Cannot resize result matrix because it is used in-place."); 1147 } 1148 1149 if (result == this || result == other) { 1150 /* actually, blas cannot do multiplications in-place. Therefore, we will fake by 1151 * allocating a temporary object on the side and copy the result later. 1152 */ 1153 ComplexDoubleMatrix temp = new ComplexDoubleMatrix(result.rows, result.columns); 1154 SimpleBlas.gemm(ComplexDouble.UNIT, this, other, ComplexDouble.ZERO, temp); 1155 SimpleBlas.copy(temp, result); 1156 } 1157 else { 1158 SimpleBlas.gemm(ComplexDouble.UNIT, this, other, ComplexDouble.ZERO, result); 1159 } 1160 return result; 1161 } 1162 1163 /** Matrix-Matrix Multiplication with a scalar (for symmetry, does the 1164 * same as muli(scalar) 1165 */ 1166 public ComplexDoubleMatrix mmuli(ComplexDouble v, ComplexDoubleMatrix result) { 1167 return muli(v, result); 1168 } 1169 1170 public ComplexDoubleMatrix mmuli(double v, ComplexDoubleMatrix result) { 1171 return muli(v, result); 1172 } 1173 1174 /** (Elementwise) division */ 1175 public ComplexDoubleMatrix divi(ComplexDoubleMatrix other, ComplexDoubleMatrix result) { 1176 if (other.isScalar()) 1177 return divi(other.scalar(), result); 1178 1179 assertSameLength(other); 1180 ensureResultLength(other, result); 1181 1182 ComplexDouble c1 = new ComplexDouble(0.0); 1183 ComplexDouble c2 = new ComplexDouble(0.0); 1184 1185 for (int i = 0; i < length; i++) 1186 result.put(i, get(i, c1).divi(other.get(i, c2))); 1187 return result; 1188 } 1189 1190 /** (Elementwise) division with a scalar */ 1191 public ComplexDoubleMatrix divi(ComplexDouble a, ComplexDoubleMatrix result) { 1192 ensureResultLength(null, result); 1193 1194 ComplexDouble c = new ComplexDouble(0.0); 1195 1196 for (int i = 0; i < length; i++) 1197 result.put(i, get(i, c).divi(a)); 1198 return result; 1199 } 1200 1201 public ComplexDoubleMatrix divi(double a, ComplexDoubleMatrix result) { 1202 return divi(new ComplexDouble(a), result); 1203 } 1204 1205 /** 1206 * (Elementwise) division, with operands switched. Computes 1207 * <em>result = other / this</em>. */ 1208 public ComplexDoubleMatrix rdivi(ComplexDoubleMatrix other, ComplexDoubleMatrix result) { 1209 if (other.isScalar()) 1210 return divi(other.scalar(), result); 1211 1212 assertSameLength(other); 1213 ensureResultLength(other, result); 1214 1215 ComplexDouble c1 = new ComplexDouble(0.0); 1216 ComplexDouble c2 = new ComplexDouble(0.0); 1217 1218 for (int i = 0; i < length; i++) 1219 result.put(i, other.get(i, c1).divi(get(i, c2))); 1220 return result; 1221 } 1222 1223 /** (Elementwise) division with a scalar, with operands switched. Computes 1224 * <em>result = a / this</em>.*/ 1225 public ComplexDoubleMatrix rdivi(ComplexDouble a, ComplexDoubleMatrix result) { 1226 ensureResultLength(null, result); 1227 1228 ComplexDouble c1 = new ComplexDouble(0.0); 1229 ComplexDouble c2 = new ComplexDouble(0.0); 1230 1231 for (int i = 0; i < length; i++) { 1232 c1.copy(a); 1233 result.put(i, c1.divi(get(i, c2))); 1234 } 1235 return result; 1236 } 1237 1238 public ComplexDoubleMatrix rdivi(double a, ComplexDoubleMatrix result) { 1239 return rdivi(new ComplexDouble(a), result); 1240 } 1241 1242 public ComplexDoubleMatrix negi() { 1243 ComplexDouble c = new ComplexDouble(0.0); 1244 for (int i = 0; i < length; i++) 1245 put(i, get(i, c).negi()); 1246 return this; 1247 } 1248 1249 public ComplexDoubleMatrix neg() { 1250 return dup().negi(); 1251 } 1252 1253 public ComplexDoubleMatrix noti() { 1254 ComplexDouble c = new ComplexDouble(0.0); 1255 for (int i = 0; i < length; i++) 1256 put(i, get(i, c).isZero() ? 1.0 : 0.0); 1257 return this; 1258 } 1259 1260 public ComplexDoubleMatrix not() { 1261 return dup().noti(); 1262 } 1263 1264 public ComplexDoubleMatrix truthi() { 1265 ComplexDouble c = new ComplexDouble(0.0); 1266 for (int i = 0; i < length; i++) 1267 put(i, get(i, c).isZero() ? 0.0 : 1.0); 1268 return this; 1269 } 1270 1271 public ComplexDoubleMatrix truth() { 1272 return dup().truthi(); 1273 } 1274 1275 /**************************************************************** 1276 * Rank one-updates 1277 */ 1278 1279 /** Computes a rank-1-update A = A + alpha * x * y'. */ 1280 public ComplexDoubleMatrix rankOneUpdate(ComplexDouble alpha, ComplexDoubleMatrix x, ComplexDoubleMatrix y) { 1281 if (rows != x.length) 1282 throw new SizeException("Vector x has wrong length (" + x.length + " != " + rows + ")."); 1283 if (columns != y.length) 1284 throw new SizeException("Vector y has wrong length (" + x.length + " != " + columns + ")."); 1285 1286 SimpleBlas.gerc(alpha, x, y, this); 1287 return this; 1288 } 1289 1290 public ComplexDoubleMatrix rankOneUpdate(double alpha, ComplexDoubleMatrix x, ComplexDoubleMatrix y) { 1291 return rankOneUpdate(new ComplexDouble(alpha), x, y); 1292 } 1293 1294 /** Computes a rank-1-update A = A + alpha * x * x'. */ 1295 public ComplexDoubleMatrix rankOneUpdate(double alpha, ComplexDoubleMatrix x) { 1296 return rankOneUpdate(new ComplexDouble(alpha), x, x); 1297 } 1298 1299 /** Computes a rank-1-update A = A + alpha * x * x'. */ 1300 public ComplexDoubleMatrix rankOneUpdate(ComplexDouble alpha, ComplexDoubleMatrix x) { 1301 return rankOneUpdate(alpha, x, x); 1302 } 1303 1304 /** Computes a rank-1-update A = A + x * x'. */ 1305 public ComplexDoubleMatrix rankOneUpdate(ComplexDoubleMatrix x) { 1306 return rankOneUpdate(1.0, x, x); 1307 } 1308 1309 /** Computes a rank-1-update A = A + x * y'. */ 1310 public ComplexDoubleMatrix rankOneUpdate(ComplexDoubleMatrix x, ComplexDoubleMatrix y) { 1311 return rankOneUpdate(1.0, x, y); 1312 } 1313 1314 /**************************************************************** 1315 * Logical operations 1316 */ 1317 1318 public ComplexDouble sum() { 1319 ComplexDouble s = new ComplexDouble(0.0); 1320 ComplexDouble c = new ComplexDouble(0.0); 1321 for (int i = 0; i < length; i++) 1322 s.addi(get(i, c)); 1323 return s; 1324 } 1325 1326 public ComplexDouble mean() { 1327 return sum().div((double)length); 1328 } 1329 1330 /** Computes this^T * other */ 1331 public ComplexDouble dotc(ComplexDoubleMatrix other) { 1332 return SimpleBlas.dotc(this, other); 1333 } 1334 1335 /** Computes this^H * other */ 1336 public ComplexDouble dotu(ComplexDoubleMatrix other) { 1337 return SimpleBlas.dotu(this, other); 1338 } 1339 1340 public double norm2() { 1341 return SimpleBlas.nrm2(this); 1342 } 1343 1344 public double normmax() { 1345 int i = SimpleBlas.iamax(this); 1346 return get(i).abs(); 1347 } 1348 1349 public double norm1() { 1350 return SimpleBlas.asum(this); 1351 } 1352 1353 /** Return a vector containing the sums of the columns (having number of columns many entries) */ 1354 public ComplexDoubleMatrix columnSums() { 1355 ComplexDoubleMatrix v = 1356 new ComplexDoubleMatrix(1, columns); 1357 1358 for (int c = 0; c < columns; c++) 1359 v.put(c, getColumn(c).sum()); 1360 1361 return v; 1362 } 1363 1364 public ComplexDoubleMatrix columnMeans() { 1365 return columnSums().divi(rows); 1366 } 1367 1368 public ComplexDoubleMatrix rowSums() { 1369 ComplexDoubleMatrix v = new ComplexDoubleMatrix(rows); 1370 1371 for (int r = 0; r < rows; r++) 1372 v.put(r, getRow(r).sum()); 1373 1374 return v; 1375 } 1376 1377 public ComplexDoubleMatrix rowMeans() { 1378 return rowSums().divi(columns); 1379 } 1380 1381 public ComplexDoubleMatrix getColumn(int c) { 1382 ComplexDoubleMatrix result = new ComplexDoubleMatrix(rows, 1); 1383 NativeBlas.zcopy(rows, data, index(0, c), 1, result.data, 0, 1); 1384 return result; 1385 } 1386 1387 public void putColumn(int c, ComplexDoubleMatrix v) { 1388 NativeBlas.zcopy(rows, v.data, 0, 1, data, index(0, c), 1); 1389 } 1390 1391 public ComplexDoubleMatrix getRow(int r) { 1392 ComplexDoubleMatrix result = new ComplexDoubleMatrix(1, columns); 1393 NativeBlas.zcopy(columns, data, index(r, 0), rows, result.data, 0, 1); 1394 return result; 1395 } 1396 1397 public void putRow(int r, ComplexDoubleMatrix v) { 1398 NativeBlas.zcopy(columns, v.data, 0, 1, data, index(r, 0), rows); 1399 } 1400 1401 /************************************************************************** 1402 * Elementwise Functions 1403 */ 1404 1405 /** Add a row vector to all rows of the matrix */ 1406 public void addRowVector(ComplexDoubleMatrix x) { 1407 for (int r = 0; r < rows; r++) { 1408 NativeBlas.zaxpy(columns, ComplexDouble.UNIT, x.data, 0, 1, data, index(r, 0), rows); 1409 } 1410 } 1411 1412 /** Add a vector to all columns of the matrix */ 1413 public void addColumnVector(ComplexDoubleMatrix x) { 1414 for (int c = 0; c < columns; c++) { 1415 NativeBlas.zaxpy(rows, ComplexDouble.UNIT, x.data, 0, 1, data, index(0, c), 1); 1416 } 1417 } 1418 1419 /** Add a row vector to all rows of the matrix */ 1420 public void subRowVector(ComplexDoubleMatrix x) { 1421 for (int r = 0; r < rows; r++) { 1422 NativeBlas.zaxpy(columns, ComplexDouble.NEG_UNIT, x.data, 0, 1, data, index(r, 0), rows); 1423 } 1424 } 1425 1426 /** Add a vector to all columns of the matrix */ 1427 public void subColumnVector(ComplexDoubleMatrix x) { 1428 for (int c = 0; c < columns; c++) { 1429 NativeBlas.zaxpy(rows, ComplexDouble.NEG_UNIT, x.data, 0, 1, data, index(0, c), 1); 1430 } 1431 } 1432 1433 /** 1434 * Writes out this matrix to the given data stream. 1435 * @param dos the data output stream to write to. 1436 * @throws IOException 1437 */ 1438 public void out(DataOutputStream dos) throws IOException { 1439 dos.writeUTF("double"); 1440 dos.writeInt(columns); 1441 dos.writeInt(rows); 1442 1443 dos.writeInt(data.length); 1444 for(int i=0; i < data.length;i++) 1445 dos.writeDouble(data[i]); 1446 } 1447 1448 /** 1449 * Reads in a matrix from the given data stream. Note 1450 * that the old data of this matrix will be discarded. 1451 * @param dis the data input stream to read from. 1452 * @throws IOException 1453 */ 1454 public void in(DataInputStream dis) throws IOException { 1455 if(!dis.readUTF().equals("double")) 1456 throw new IllegalStateException("The matrix in the specified file is not of the correct type!"); 1457 1458 this.columns = dis.readInt(); 1459 this.rows = dis.readInt(); 1460 1461 final int MAX = dis.readInt(); 1462 data = new double[MAX]; 1463 for(int i=0; i < MAX;i++) 1464 data[i] = dis.readDouble(); 1465 } 1466 1467 /** 1468 * Saves this matrix to the specified file. 1469 * @param filename the file to write the matrix in. 1470 * @throws IOException thrown on errors while writing the matrix to the file 1471 */ 1472 public void save(String filename) throws IOException { 1473 DataOutputStream dos = new DataOutputStream(new FileOutputStream(filename, false)); 1474 this.out(dos); 1475 } 1476 1477 /** 1478 * Loads a matrix from a file into this matrix. Note that the old data 1479 * of this matrix will be discarded. 1480 * @param filename the file to read the matrix from 1481 * @throws IOException thrown on errors while reading the matrix 1482 */ 1483 public void load(String filename) throws IOException { 1484 DataInputStream dis = new DataInputStream(new FileInputStream(filename)); 1485 this.in(dis); 1486 } 1487 1488 /**************************************************************** 1489 * Autogenerated code 1490 */ 1491 1492 /***** Code for operators ***************************************/ 1493 1494 /* Overloads for the usual arithmetic operations */ 1495 /*# 1496 def gen_overloads(base, result_rows, result_cols); <<-EOS 1497 public ComplexDoubleMatrix #{base}i(ComplexDoubleMatrix other) { 1498 return #{base}i(other, this); 1499 } 1500 1501 public ComplexDoubleMatrix #{base}(ComplexDoubleMatrix other) { 1502 return #{base}i(other, new ComplexDoubleMatrix(#{result_rows}, #{result_cols})); 1503 } 1504 1505 public ComplexDoubleMatrix #{base}i(ComplexDouble v) { 1506 return #{base}i(v, this); 1507 } 1508 1509 public ComplexDoubleMatrix #{base}i(double v) { 1510 return #{base}i(new ComplexDouble(v), this); 1511 } 1512 1513 public ComplexDoubleMatrix #{base}(ComplexDouble v) { 1514 return #{base}i(v, new ComplexDoubleMatrix(rows, columns)); 1515 } 1516 1517 public ComplexDoubleMatrix #{base}(double v) { 1518 return #{base}i(new ComplexDouble(v), new ComplexDoubleMatrix(rows, columns)); 1519 } 1520 1521 EOS 1522 end 1523 #*/ 1524 1525 /* Generating code for logical operators. This not only generates the stubs 1526 * but really all of the code. 1527 */ 1528 1529 /*# 1530 def gen_compare(name, op); <<-EOS 1531 public ComplexDoubleMatrix #{name}i(ComplexDoubleMatrix other, ComplexDoubleMatrix result) { 1532 if (other.isScalar()) 1533 return #{name}i(other.scalar(), result); 1534 1535 assertSameLength(other); 1536 ensureResultLength(other, result); 1537 1538 ComplexDouble c1 = new ComplexDouble(0.0); 1539 ComplexDouble c2 = new ComplexDouble(0.0); 1540 1541 for (int i = 0; i < length; i++) 1542 result.put(i, get(i, c1).#{op}(other.get(i, c2)) ? 1.0 : 0.0); 1543 return result; 1544 } 1545 1546 public ComplexDoubleMatrix #{name}i(ComplexDoubleMatrix other) { 1547 return #{name}i(other, this); 1548 } 1549 1550 public ComplexDoubleMatrix #{name}(ComplexDoubleMatrix other) { 1551 return #{name}i(other, new ComplexDoubleMatrix(rows, columns)); 1552 } 1553 1554 public ComplexDoubleMatrix #{name}i(ComplexDouble value, ComplexDoubleMatrix result) { 1555 ensureResultLength(null, result); 1556 ComplexDouble c = new ComplexDouble(0.0); 1557 for (int i = 0; i < length; i++) 1558 result.put(i, get(i, c).#{op}(value) ? 1.0 : 0.0); 1559 return result; 1560 } 1561 1562 public ComplexDoubleMatrix #{name}i(double value, ComplexDoubleMatrix result) { 1563 return #{name}i(new ComplexDouble(value), result); 1564 } 1565 1566 public ComplexDoubleMatrix #{name}i(ComplexDouble value) { 1567 return #{name}i(value, this); 1568 } 1569 1570 public ComplexDoubleMatrix #{name}i(double value) { 1571 return #{name}i(new ComplexDouble(value)); 1572 } 1573 1574 public ComplexDoubleMatrix #{name}(ComplexDouble value) { 1575 return #{name}i(value, new ComplexDoubleMatrix(rows, columns)); 1576 } 1577 1578 public ComplexDoubleMatrix #{name}(double value) { 1579 return #{name}i(new ComplexDouble(value)); 1580 } 1581 1582 EOS 1583 end 1584 #*/ 1585 1586 /*# 1587 def gen_logical(name, op); <<-EOS 1588 public ComplexDoubleMatrix #{name}i(ComplexDoubleMatrix other, ComplexDoubleMatrix result) { 1589 assertSameLength(other); 1590 ensureResultLength(other, result); 1591 1592 ComplexDouble t1 = new ComplexDouble(0.0); 1593 ComplexDouble t2 = new ComplexDouble(0.0); 1594 1595 for (int i = 0; i < length; i++) 1596 result.put(i, (!get(i, t1).isZero()) #{op} (!other.get(i, t2).isZero()) ? 1.0 : 0.0); 1597 return result; 1598 } 1599 1600 public ComplexDoubleMatrix #{name}i(ComplexDoubleMatrix other) { 1601 return #{name}i(other, this); 1602 } 1603 1604 public ComplexDoubleMatrix #{name}(ComplexDoubleMatrix other) { 1605 return #{name}i(other, new ComplexDoubleMatrix(rows, columns)); 1606 } 1607 1608 public ComplexDoubleMatrix #{name}i(ComplexDouble value, ComplexDoubleMatrix result) { 1609 ensureResultLength(null, result); 1610 boolean val = !value.isZero(); 1611 ComplexDouble t = new ComplexDouble(0.0); 1612 for (int i = 0; i < length; i++) 1613 result.put(i, !get(i, t).isZero() #{op} val ? 1.0 : 0.0); 1614 return result; 1615 } 1616 1617 public ComplexDoubleMatrix #{name}i(double value, ComplexDoubleMatrix result) { 1618 return #{name}i(new ComplexDouble(value), result); 1619 } 1620 1621 public ComplexDoubleMatrix #{name}i(ComplexDouble value) { 1622 return #{name}i(value, this); 1623 } 1624 1625 public ComplexDoubleMatrix #{name}i(double value) { 1626 return #{name}i(new ComplexDouble(value), this); 1627 } 1628 1629 public ComplexDoubleMatrix #{name}(ComplexDouble value) { 1630 return #{name}i(value, new ComplexDoubleMatrix(rows, columns)); 1631 } 1632 1633 public ComplexDoubleMatrix #{name}(double value) { 1634 return #{name}i(new ComplexDouble(value)); 1635 } 1636 EOS 1637 end 1638 #*/ 1639 1640 /*# collect(gen_overloads('add', 'rows', 'columns'), 1641 gen_overloads('sub', 'rows', 'columns'), 1642 gen_overloads('rsub', 'rows', 'columns'), 1643 gen_overloads('div', 'rows', 'columns'), 1644 gen_overloads('rdiv', 'rows', 'columns'), 1645 gen_overloads('mul', 'rows', 'columns'), 1646 gen_overloads('mmul', 'rows', 'other.columns'), 1647 gen_compare('eq', 'eq'), 1648 gen_compare('ne', 'eq'), 1649 gen_logical('and', '&'), 1650 gen_logical('or', '|'), 1651 gen_logical('xor', '^')) 1652 #*/ 1653//RJPP-BEGIN------------------------------------------------------------ 1654 public ComplexDoubleMatrix addi(ComplexDoubleMatrix other) { 1655 return addi(other, this); 1656 } 1657 1658 public ComplexDoubleMatrix add(ComplexDoubleMatrix other) { 1659 return addi(other, new ComplexDoubleMatrix(rows, columns)); 1660 } 1661 1662 public ComplexDoubleMatrix addi(ComplexDouble v) { 1663 return addi(v, this); 1664 } 1665 1666 public ComplexDoubleMatrix addi(double v) { 1667 return addi(new ComplexDouble(v), this); 1668 } 1669 1670 public ComplexDoubleMatrix add(ComplexDouble v) { 1671 return addi(v, new ComplexDoubleMatrix(rows, columns)); 1672 } 1673 1674 public ComplexDoubleMatrix add(double v) { 1675 return addi(new ComplexDouble(v), new ComplexDoubleMatrix(rows, columns)); 1676 } 1677 1678 1679 public ComplexDoubleMatrix subi(ComplexDoubleMatrix other) { 1680 return subi(other, this); 1681 } 1682 1683 public ComplexDoubleMatrix sub(ComplexDoubleMatrix other) { 1684 return subi(other, new ComplexDoubleMatrix(rows, columns)); 1685 } 1686 1687 public ComplexDoubleMatrix subi(ComplexDouble v) { 1688 return subi(v, this); 1689 } 1690 1691 public ComplexDoubleMatrix subi(double v) { 1692 return subi(new ComplexDouble(v), this); 1693 } 1694 1695 public ComplexDoubleMatrix sub(ComplexDouble v) { 1696 return subi(v, new ComplexDoubleMatrix(rows, columns)); 1697 } 1698 1699 public ComplexDoubleMatrix sub(double v) { 1700 return subi(new ComplexDouble(v), new ComplexDoubleMatrix(rows, columns)); 1701 } 1702 1703 1704 public ComplexDoubleMatrix rsubi(ComplexDoubleMatrix other) { 1705 return rsubi(other, this); 1706 } 1707 1708 public ComplexDoubleMatrix rsub(ComplexDoubleMatrix other) { 1709 return rsubi(other, new ComplexDoubleMatrix(rows, columns)); 1710 } 1711 1712 public ComplexDoubleMatrix rsubi(ComplexDouble v) { 1713 return rsubi(v, this); 1714 } 1715 1716 public ComplexDoubleMatrix rsubi(double v) { 1717 return rsubi(new ComplexDouble(v), this); 1718 } 1719 1720 public ComplexDoubleMatrix rsub(ComplexDouble v) { 1721 return rsubi(v, new ComplexDoubleMatrix(rows, columns)); 1722 } 1723 1724 public ComplexDoubleMatrix rsub(double v) { 1725 return rsubi(new ComplexDouble(v), new ComplexDoubleMatrix(rows, columns)); 1726 } 1727 1728 1729 public ComplexDoubleMatrix divi(ComplexDoubleMatrix other) { 1730 return divi(other, this); 1731 } 1732 1733 public ComplexDoubleMatrix div(ComplexDoubleMatrix other) { 1734 return divi(other, new ComplexDoubleMatrix(rows, columns)); 1735 } 1736 1737 public ComplexDoubleMatrix divi(ComplexDouble v) { 1738 return divi(v, this); 1739 } 1740 1741 public ComplexDoubleMatrix divi(double v) { 1742 return divi(new ComplexDouble(v), this); 1743 } 1744 1745 public ComplexDoubleMatrix div(ComplexDouble v) { 1746 return divi(v, new ComplexDoubleMatrix(rows, columns)); 1747 } 1748 1749 public ComplexDoubleMatrix div(double v) { 1750 return divi(new ComplexDouble(v), new ComplexDoubleMatrix(rows, columns)); 1751 } 1752 1753 1754 public ComplexDoubleMatrix rdivi(ComplexDoubleMatrix other) { 1755 return rdivi(other, this); 1756 } 1757 1758 public ComplexDoubleMatrix rdiv(ComplexDoubleMatrix other) { 1759 return rdivi(other, new ComplexDoubleMatrix(rows, columns)); 1760 } 1761 1762 public ComplexDoubleMatrix rdivi(ComplexDouble v) { 1763 return rdivi(v, this); 1764 } 1765 1766 public ComplexDoubleMatrix rdivi(double v) { 1767 return rdivi(new ComplexDouble(v), this); 1768 } 1769 1770 public ComplexDoubleMatrix rdiv(ComplexDouble v) { 1771 return rdivi(v, new ComplexDoubleMatrix(rows, columns)); 1772 } 1773 1774 public ComplexDoubleMatrix rdiv(double v) { 1775 return rdivi(new ComplexDouble(v), new ComplexDoubleMatrix(rows, columns)); 1776 } 1777 1778 1779 public ComplexDoubleMatrix muli(ComplexDoubleMatrix other) { 1780 return muli(other, this); 1781 } 1782 1783 public ComplexDoubleMatrix mul(ComplexDoubleMatrix other) { 1784 return muli(other, new ComplexDoubleMatrix(rows, columns)); 1785 } 1786 1787 public ComplexDoubleMatrix muli(ComplexDouble v) { 1788 return muli(v, this); 1789 } 1790 1791 public ComplexDoubleMatrix muli(double v) { 1792 return muli(new ComplexDouble(v), this); 1793 } 1794 1795 public ComplexDoubleMatrix mul(ComplexDouble v) { 1796 return muli(v, new ComplexDoubleMatrix(rows, columns)); 1797 } 1798 1799 public ComplexDoubleMatrix mul(double v) { 1800 return muli(new ComplexDouble(v), new ComplexDoubleMatrix(rows, columns)); 1801 } 1802 1803 1804 public ComplexDoubleMatrix mmuli(ComplexDoubleMatrix other) { 1805 return mmuli(other, this); 1806 } 1807 1808 public ComplexDoubleMatrix mmul(ComplexDoubleMatrix other) { 1809 return mmuli(other, new ComplexDoubleMatrix(rows, other.columns)); 1810 } 1811 1812 public ComplexDoubleMatrix mmuli(ComplexDouble v) { 1813 return mmuli(v, this); 1814 } 1815 1816 public ComplexDoubleMatrix mmuli(double v) { 1817 return mmuli(new ComplexDouble(v), this); 1818 } 1819 1820 public ComplexDoubleMatrix mmul(ComplexDouble v) { 1821 return mmuli(v, new ComplexDoubleMatrix(rows, columns)); 1822 } 1823 1824 public ComplexDoubleMatrix mmul(double v) { 1825 return mmuli(new ComplexDouble(v), new ComplexDoubleMatrix(rows, columns)); 1826 } 1827 1828 1829 public ComplexDoubleMatrix eqi(ComplexDoubleMatrix other, ComplexDoubleMatrix result) { 1830 if (other.isScalar()) 1831 return eqi(other.scalar(), result); 1832 1833 assertSameLength(other); 1834 ensureResultLength(other, result); 1835 1836 ComplexDouble c1 = new ComplexDouble(0.0); 1837 ComplexDouble c2 = new ComplexDouble(0.0); 1838 1839 for (int i = 0; i < length; i++) 1840 result.put(i, get(i, c1).eq(other.get(i, c2)) ? 1.0 : 0.0); 1841 return result; 1842 } 1843 1844 public ComplexDoubleMatrix eqi(ComplexDoubleMatrix other) { 1845 return eqi(other, this); 1846 } 1847 1848 public ComplexDoubleMatrix eq(ComplexDoubleMatrix other) { 1849 return eqi(other, new ComplexDoubleMatrix(rows, columns)); 1850 } 1851 1852 public ComplexDoubleMatrix eqi(ComplexDouble value, ComplexDoubleMatrix result) { 1853 ensureResultLength(null, result); 1854 ComplexDouble c = new ComplexDouble(0.0); 1855 for (int i = 0; i < length; i++) 1856 result.put(i, get(i, c).eq(value) ? 1.0 : 0.0); 1857 return result; 1858 } 1859 1860 public ComplexDoubleMatrix eqi(double value, ComplexDoubleMatrix result) { 1861 return eqi(new ComplexDouble(value), result); 1862 } 1863 1864 public ComplexDoubleMatrix eqi(ComplexDouble value) { 1865 return eqi(value, this); 1866 } 1867 1868 public ComplexDoubleMatrix eqi(double value) { 1869 return eqi(new ComplexDouble(value)); 1870 } 1871 1872 public ComplexDoubleMatrix eq(ComplexDouble value) { 1873 return eqi(value, new ComplexDoubleMatrix(rows, columns)); 1874 } 1875 1876 public ComplexDoubleMatrix eq(double value) { 1877 return eqi(new ComplexDouble(value)); 1878 } 1879 1880 1881 public ComplexDoubleMatrix nei(ComplexDoubleMatrix other, ComplexDoubleMatrix result) { 1882 if (other.isScalar()) 1883 return nei(other.scalar(), result); 1884 1885 assertSameLength(other); 1886 ensureResultLength(other, result); 1887 1888 ComplexDouble c1 = new ComplexDouble(0.0); 1889 ComplexDouble c2 = new ComplexDouble(0.0); 1890 1891 for (int i = 0; i < length; i++) 1892 result.put(i, get(i, c1).eq(other.get(i, c2)) ? 1.0 : 0.0); 1893 return result; 1894 } 1895 1896 public ComplexDoubleMatrix nei(ComplexDoubleMatrix other) { 1897 return nei(other, this); 1898 } 1899 1900 public ComplexDoubleMatrix ne(ComplexDoubleMatrix other) { 1901 return nei(other, new ComplexDoubleMatrix(rows, columns)); 1902 } 1903 1904 public ComplexDoubleMatrix nei(ComplexDouble value, ComplexDoubleMatrix result) { 1905 ensureResultLength(null, result); 1906 ComplexDouble c = new ComplexDouble(0.0); 1907 for (int i = 0; i < length; i++) 1908 result.put(i, get(i, c).eq(value) ? 1.0 : 0.0); 1909 return result; 1910 } 1911 1912 public ComplexDoubleMatrix nei(double value, ComplexDoubleMatrix result) { 1913 return nei(new ComplexDouble(value), result); 1914 } 1915 1916 public ComplexDoubleMatrix nei(ComplexDouble value) { 1917 return nei(value, this); 1918 } 1919 1920 public ComplexDoubleMatrix nei(double value) { 1921 return nei(new ComplexDouble(value)); 1922 } 1923 1924 public ComplexDoubleMatrix ne(ComplexDouble value) { 1925 return nei(value, new ComplexDoubleMatrix(rows, columns)); 1926 } 1927 1928 public ComplexDoubleMatrix ne(double value) { 1929 return nei(new ComplexDouble(value)); 1930 } 1931 1932 1933 public ComplexDoubleMatrix andi(ComplexDoubleMatrix other, ComplexDoubleMatrix result) { 1934 assertSameLength(other); 1935 ensureResultLength(other, result); 1936 1937 ComplexDouble t1 = new ComplexDouble(0.0); 1938 ComplexDouble t2 = new ComplexDouble(0.0); 1939 1940 for (int i = 0; i < length; i++) 1941 result.put(i, (!get(i, t1).isZero()) & (!other.get(i, t2).isZero()) ? 1.0 : 0.0); 1942 return result; 1943 } 1944 1945 public ComplexDoubleMatrix andi(ComplexDoubleMatrix other) { 1946 return andi(other, this); 1947 } 1948 1949 public ComplexDoubleMatrix and(ComplexDoubleMatrix other) { 1950 return andi(other, new ComplexDoubleMatrix(rows, columns)); 1951 } 1952 1953 public ComplexDoubleMatrix andi(ComplexDouble value, ComplexDoubleMatrix result) { 1954 ensureResultLength(null, result); 1955 boolean val = !value.isZero(); 1956 ComplexDouble t = new ComplexDouble(0.0); 1957 for (int i = 0; i < length; i++) 1958 result.put(i, !get(i, t).isZero() & val ? 1.0 : 0.0); 1959 return result; 1960 } 1961 1962 public ComplexDoubleMatrix andi(double value, ComplexDoubleMatrix result) { 1963 return andi(new ComplexDouble(value), result); 1964 } 1965 1966 public ComplexDoubleMatrix andi(ComplexDouble value) { 1967 return andi(value, this); 1968 } 1969 1970 public ComplexDoubleMatrix andi(double value) { 1971 return andi(new ComplexDouble(value), this); 1972 } 1973 1974 public ComplexDoubleMatrix and(ComplexDouble value) { 1975 return andi(value, new ComplexDoubleMatrix(rows, columns)); 1976 } 1977 1978 public ComplexDoubleMatrix and(double value) { 1979 return andi(new ComplexDouble(value)); 1980 } 1981 1982 public ComplexDoubleMatrix ori(ComplexDoubleMatrix other, ComplexDoubleMatrix result) { 1983 assertSameLength(other); 1984 ensureResultLength(other, result); 1985 1986 ComplexDouble t1 = new ComplexDouble(0.0); 1987 ComplexDouble t2 = new ComplexDouble(0.0); 1988 1989 for (int i = 0; i < length; i++) 1990 result.put(i, (!get(i, t1).isZero()) | (!other.get(i, t2).isZero()) ? 1.0 : 0.0); 1991 return result; 1992 } 1993 1994 public ComplexDoubleMatrix ori(ComplexDoubleMatrix other) { 1995 return ori(other, this); 1996 } 1997 1998 public ComplexDoubleMatrix or(ComplexDoubleMatrix other) { 1999 return ori(other, new ComplexDoubleMatrix(rows, columns)); 2000 } 2001 2002 public ComplexDoubleMatrix ori(ComplexDouble value, ComplexDoubleMatrix result) { 2003 ensureResultLength(null, result); 2004 boolean val = !value.isZero(); 2005 ComplexDouble t = new ComplexDouble(0.0); 2006 for (int i = 0; i < length; i++) 2007 result.put(i, !get(i, t).isZero() | val ? 1.0 : 0.0); 2008 return result; 2009 } 2010 2011 public ComplexDoubleMatrix ori(double value, ComplexDoubleMatrix result) { 2012 return ori(new ComplexDouble(value), result); 2013 } 2014 2015 public ComplexDoubleMatrix ori(ComplexDouble value) { 2016 return ori(value, this); 2017 } 2018 2019 public ComplexDoubleMatrix ori(double value) { 2020 return ori(new ComplexDouble(value), this); 2021 } 2022 2023 public ComplexDoubleMatrix or(ComplexDouble value) { 2024 return ori(value, new ComplexDoubleMatrix(rows, columns)); 2025 } 2026 2027 public ComplexDoubleMatrix or(double value) { 2028 return ori(new ComplexDouble(value)); 2029 } 2030 2031 public ComplexDoubleMatrix xori(ComplexDoubleMatrix other, ComplexDoubleMatrix result) { 2032 assertSameLength(other); 2033 ensureResultLength(other, result); 2034 2035 ComplexDouble t1 = new ComplexDouble(0.0); 2036 ComplexDouble t2 = new ComplexDouble(0.0); 2037 2038 for (int i = 0; i < length; i++) 2039 result.put(i, (!get(i, t1).isZero()) ^ (!other.get(i, t2).isZero()) ? 1.0 : 0.0); 2040 return result; 2041 } 2042 2043 public ComplexDoubleMatrix xori(ComplexDoubleMatrix other) { 2044 return xori(other, this); 2045 } 2046 2047 public ComplexDoubleMatrix xor(ComplexDoubleMatrix other) { 2048 return xori(other, new ComplexDoubleMatrix(rows, columns)); 2049 } 2050 2051 public ComplexDoubleMatrix xori(ComplexDouble value, ComplexDoubleMatrix result) { 2052 ensureResultLength(null, result); 2053 boolean val = !value.isZero(); 2054 ComplexDouble t = new ComplexDouble(0.0); 2055 for (int i = 0; i < length; i++) 2056 result.put(i, !get(i, t).isZero() ^ val ? 1.0 : 0.0); 2057 return result; 2058 } 2059 2060 public ComplexDoubleMatrix xori(double value, ComplexDoubleMatrix result) { 2061 return xori(new ComplexDouble(value), result); 2062 } 2063 2064 public ComplexDoubleMatrix xori(ComplexDouble value) { 2065 return xori(value, this); 2066 } 2067 2068 public ComplexDoubleMatrix xori(double value) { 2069 return xori(new ComplexDouble(value), this); 2070 } 2071 2072 public ComplexDoubleMatrix xor(ComplexDouble value) { 2073 return xori(value, new ComplexDoubleMatrix(rows, columns)); 2074 } 2075 2076 public ComplexDoubleMatrix xor(double value) { 2077 return xori(new ComplexDouble(value)); 2078 } 2079//RJPP-END-------------------------------------------------------------- 2080}