001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.core; 025 026 import java.io.File; 027 import java.io.IOException; 028 import java.io.InputStream; 029 import java.lang.reflect.Constructor; 030 import java.net.URL; 031 import java.util.Arrays; 032 import java.util.Collection; 033 import java.util.Date; 034 import java.util.Map; 035 036 import org.ujmp.core.bigintegermatrix.calculation.Fibonacci; 037 import org.ujmp.core.booleanmatrix.DenseBooleanMatrix2D; 038 import org.ujmp.core.booleanmatrix.impl.DefaultDenseBooleanMatrix2D; 039 import org.ujmp.core.bytematrix.DenseByteMatrix2D; 040 import org.ujmp.core.bytematrix.impl.ArrayDenseByteMatrix2D; 041 import org.ujmp.core.calculation.Calculation.Ret; 042 import org.ujmp.core.charmatrix.DenseCharMatrix2D; 043 import org.ujmp.core.charmatrix.impl.ArrayDenseCharMatrix2D; 044 import org.ujmp.core.datematrix.DenseDateMatrix2D; 045 import org.ujmp.core.datematrix.impl.SimpleDenseDateMatrix2D; 046 import org.ujmp.core.doublematrix.DenseDoubleMatrix; 047 import org.ujmp.core.doublematrix.DenseDoubleMatrix2D; 048 import org.ujmp.core.doublematrix.calculation.entrywise.creators.Eye; 049 import org.ujmp.core.doublematrix.calculation.entrywise.creators.Ones; 050 import org.ujmp.core.doublematrix.calculation.entrywise.creators.Pascal; 051 import org.ujmp.core.doublematrix.calculation.entrywise.creators.Rand; 052 import org.ujmp.core.doublematrix.calculation.entrywise.creators.Randn; 053 import org.ujmp.core.doublematrix.calculation.entrywise.creators.Range; 054 import org.ujmp.core.doublematrix.calculation.general.misc.Dense2Sparse; 055 import org.ujmp.core.doublematrix.impl.ArrayDenseDoubleMatrix2D; 056 import org.ujmp.core.doublematrix.impl.DenseFileMatrix; 057 import org.ujmp.core.enums.DB; 058 import org.ujmp.core.enums.FileFormat; 059 import org.ujmp.core.enums.ValueType; 060 import org.ujmp.core.exceptions.MatrixException; 061 import org.ujmp.core.floatmatrix.DenseFloatMatrix2D; 062 import org.ujmp.core.floatmatrix.impl.ArrayDenseFloatMatrix2D; 063 import org.ujmp.core.genericmatrix.GenericMatrix; 064 import org.ujmp.core.intmatrix.DenseIntMatrix2D; 065 import org.ujmp.core.intmatrix.calculation.Magic; 066 import org.ujmp.core.intmatrix.impl.SimpleDenseIntMatrix2D; 067 import org.ujmp.core.io.ImportMatrix; 068 import org.ujmp.core.io.ImportMatrixJDBC; 069 import org.ujmp.core.io.LinkMatrix; 070 import org.ujmp.core.io.LinkMatrixJDBC; 071 import org.ujmp.core.listmatrix.DefaultListMatrix; 072 import org.ujmp.core.longmatrix.DenseLongMatrix2D; 073 import org.ujmp.core.longmatrix.impl.DefaultDenseLongMatrix2D; 074 import org.ujmp.core.longmatrix.impl.SimpleDenseLongMatrix2D; 075 import org.ujmp.core.mapmatrix.DefaultMapMatrix; 076 import org.ujmp.core.mapper.MatrixMapper; 077 import org.ujmp.core.matrix.DenseMatrix; 078 import org.ujmp.core.matrix.SparseMatrix; 079 import org.ujmp.core.objectmatrix.DenseObjectMatrix2D; 080 import org.ujmp.core.objectmatrix.ObjectMatrix2D; 081 import org.ujmp.core.objectmatrix.calculation.Concatenation; 082 import org.ujmp.core.objectmatrix.calculation.Convert; 083 import org.ujmp.core.objectmatrix.calculation.Fill; 084 import org.ujmp.core.objectmatrix.calculation.Repmat; 085 import org.ujmp.core.objectmatrix.calculation.WelcomeMatrix; 086 import org.ujmp.core.objectmatrix.impl.EmptyMatrix; 087 import org.ujmp.core.objectmatrix.impl.SimpleDenseObjectMatrix2D; 088 import org.ujmp.core.objectmatrix.impl.SynchronizedGenericMatrix; 089 import org.ujmp.core.shortmatrix.DenseShortMatrix2D; 090 import org.ujmp.core.shortmatrix.impl.SimpleDenseShortMatrix2D; 091 import org.ujmp.core.stringmatrix.DenseStringMatrix2D; 092 import org.ujmp.core.stringmatrix.impl.FileListMatrix; 093 import org.ujmp.core.stringmatrix.impl.SimpleDenseStringMatrix2D; 094 import org.ujmp.core.util.MathUtil; 095 import org.ujmp.core.util.matrices.MatrixAvailableProcessors; 096 import org.ujmp.core.util.matrices.MatrixMemoryUsage; 097 import org.ujmp.core.util.matrices.MatrixRandomSeed; 098 import org.ujmp.core.util.matrices.MatrixRunningThreads; 099 import org.ujmp.core.util.matrices.MatrixSystemEnvironment; 100 import org.ujmp.core.util.matrices.MatrixSystemProperties; 101 import org.ujmp.core.util.matrices.MatrixSystemTime; 102 103 /** 104 * This class provides a factory for matrix generation. Use 105 * <code>Matrix.factory.dense(rows, columns)</code> or 106 * <code>Matrix.factory.sparse(rows, columns)</code> to create empty matrices. 107 * 108 * 109 * 110 * 111 * @author Holger Arndt 112 */ 113 public abstract class MatrixFactory { 114 115 public static final int ROW = Matrix.ROW; 116 117 public static final int COLUMN = Matrix.COLUMN; 118 119 public static final int Y = Matrix.Y; 120 121 public static final int X = Matrix.X; 122 123 public static final int Z = Matrix.Z; 124 125 public static final int ALL = Matrix.ALL; 126 127 public static final int NONE = Matrix.NONE; 128 129 public static final EmptyMatrix EMPTYMATRIX = new EmptyMatrix(); 130 131 private static MatrixMapper matrixMapper = MatrixMapper.getInstance(); 132 133 public static final Matrix systemTime() { 134 return new MatrixSystemTime(); 135 } 136 137 public static final Matrix availableProcessors() { 138 return new MatrixAvailableProcessors(); 139 } 140 141 public static final Matrix memoryUsage() { 142 return new MatrixMemoryUsage(); 143 } 144 145 public static final Matrix range(double start, double end, double stepSize) { 146 return new Range(null, start, stepSize, end).calc(Ret.LINK); 147 } 148 149 public static final Matrix range(double start, double end) { 150 return range(start, 1.0, end); 151 } 152 153 public static final Matrix randomSeed() { 154 return new MatrixRandomSeed(); 155 } 156 157 public static final Matrix runningThreads() { 158 return new MatrixRunningThreads(); 159 } 160 161 public static final Matrix systemEnvironment() { 162 return new MatrixSystemEnvironment(); 163 } 164 165 public static final Matrix systemProperties() { 166 return new MatrixSystemProperties(); 167 } 168 169 public static final Matrix horCat(Matrix... matrices) throws MatrixException { 170 return concat(COLUMN, matrices); 171 } 172 173 public static final <A> Matrix vertCat(Matrix... matrices) throws MatrixException { 174 return concat(ROW, matrices); 175 } 176 177 public static final <A> Matrix vertCat(Collection<Matrix> matrices) throws MatrixException { 178 return concat(ROW, matrices); 179 } 180 181 public static final Matrix horCat(Collection<Matrix> matrices) throws MatrixException { 182 return concat(COLUMN, matrices); 183 } 184 185 public static final Matrix concat(int dimension, Matrix... matrices) throws MatrixException { 186 return concat(dimension, Arrays.asList(matrices)); 187 } 188 189 public static final Matrix concat(int dimension, Collection<Matrix> matrices) 190 throws MatrixException { 191 return new Concatenation(dimension, matrices).calc(Ret.NEW); 192 } 193 194 public static final Matrix importFromArray(boolean[]... values) { 195 return linkToArray(values).clone(); 196 } 197 198 public static final Matrix importFromArray(byte[]... values) { 199 return linkToArray(values).clone(); 200 } 201 202 public static final Matrix importFromArray(char[]... values) { 203 return linkToArray(values).clone(); 204 } 205 206 public static final Matrix importFromArray(Date[]... values) { 207 return linkToArray(values).clone(); 208 } 209 210 public static final Matrix importFromArray(double[]... values) { 211 return linkToArray(values).clone(); 212 } 213 214 public static final Matrix importFromArray(float[]... values) { 215 return linkToArray(values).clone(); 216 } 217 218 public static final Matrix importFromArray(int[]... values) { 219 return linkToArray(values).clone(); 220 } 221 222 public static final Matrix importFromArray(long[]... values) { 223 return linkToArray(values).clone(); 224 } 225 226 public static final Matrix importFromArray(Object[]... values) { 227 return linkToArray(values).clone(); 228 } 229 230 public static final Matrix importFromArray(short[]... values) { 231 return linkToArray(values).clone(); 232 } 233 234 public static final Matrix importFromArray(String[]... values) { 235 return linkToArray(values).clone(); 236 } 237 238 public static final DenseBooleanMatrix2D linkToArray(boolean[]... values) { 239 return new DefaultDenseBooleanMatrix2D(values); 240 } 241 242 public static final DenseBooleanMatrix2D linkToArray(boolean... values) { 243 return new DefaultDenseBooleanMatrix2D(values); 244 } 245 246 public static final DenseByteMatrix2D linkToArray(byte[]... values) { 247 return new ArrayDenseByteMatrix2D(values); 248 } 249 250 public static final DenseByteMatrix2D linkToArray(byte... values) { 251 return new ArrayDenseByteMatrix2D(values); 252 } 253 254 public static final DenseCharMatrix2D linkToArray(char[]... values) { 255 return new ArrayDenseCharMatrix2D(values); 256 } 257 258 public static final DenseCharMatrix2D linkToArray(char... values) { 259 return new ArrayDenseCharMatrix2D(values); 260 } 261 262 public static final DenseDateMatrix2D linkToArray(Date[]... values) { 263 return new SimpleDenseDateMatrix2D(values); 264 } 265 266 public static final DenseDateMatrix2D linkToArray(Date... values) { 267 return new SimpleDenseDateMatrix2D(values); 268 } 269 270 public static final DenseDoubleMatrix2D linkToArray(double[]... values) { 271 return new ArrayDenseDoubleMatrix2D(values); 272 } 273 274 public static final DenseDoubleMatrix2D linkToArray(double... values) { 275 return new ArrayDenseDoubleMatrix2D(values); 276 } 277 278 public static final DenseFloatMatrix2D linkToArray(float[]... values) { 279 return new ArrayDenseFloatMatrix2D(values); 280 } 281 282 public static final DenseFloatMatrix2D linkToArray(float... values) { 283 return new ArrayDenseFloatMatrix2D(values); 284 } 285 286 public static final DenseIntMatrix2D linkToArray(int[]... values) { 287 return new SimpleDenseIntMatrix2D(values); 288 } 289 290 public static final DenseIntMatrix2D linkToArray(int... values) { 291 return new SimpleDenseIntMatrix2D(values); 292 } 293 294 public static final DenseLongMatrix2D linkToArray(long[]... values) { 295 return new SimpleDenseLongMatrix2D(values); 296 } 297 298 public static final DenseLongMatrix2D linkToArray(long... values) { 299 return new DefaultDenseLongMatrix2D(values); 300 } 301 302 public static final DenseObjectMatrix2D linkToArray(Object[]... values) { 303 return new SimpleDenseObjectMatrix2D(values); 304 } 305 306 public static final DenseObjectMatrix2D linkToArray(Object... values) { 307 return new SimpleDenseObjectMatrix2D(values); 308 } 309 310 public static final DenseShortMatrix2D linkToArray(short[]... values) { 311 return new SimpleDenseShortMatrix2D(values); 312 } 313 314 public static final DenseShortMatrix2D linkToArray(short... values) { 315 return new SimpleDenseShortMatrix2D(values); 316 } 317 318 public static final DenseStringMatrix2D linkToArray(String[]... values) { 319 return new SimpleDenseStringMatrix2D(values); 320 } 321 322 public static final DenseStringMatrix2D linkToArray(String... values) { 323 return new SimpleDenseStringMatrix2D(values); 324 } 325 326 /** 327 * @deprecated Please do not use this method anymore, it will be removed. 328 * use <code>matrix.clone()</code> instead 329 */ 330 public static final Matrix copyFromMatrix(Matrix matrix) throws MatrixException { 331 return Convert.calcNew(matrix); 332 } 333 334 /** 335 * @deprecated Please do not use this method anymore, it will be moved to 336 * <code>DenseMatrix.factory.rand</code> 337 */ 338 public static final Matrix randn(long... size) throws MatrixException { 339 return Randn.calc(size); 340 } 341 342 public static final Matrix randn(ValueType valueType, long... size) throws MatrixException { 343 return Randn.calc(valueType, size); 344 } 345 346 /** 347 * @deprecated Please do not use this method anymore, it will be moved to 348 * <code>DenseMatrix.factory.rand</code> 349 */ 350 public static final Matrix rand(long... size) throws MatrixException { 351 return Rand.calc(size); 352 } 353 354 public static final Matrix rand(ValueType valueType, long... size) throws MatrixException { 355 return Rand.calc(valueType, size); 356 } 357 358 public static final Matrix correlatedColumns(int rows, int columns, double correlationFactor) 359 throws MatrixException { 360 Matrix ret = Matrix.factory.zeros(rows, columns); 361 362 Matrix orig = MatrixFactory.randn(rows, 1); 363 364 for (int c = 0; c < columns; c++) { 365 Matrix rand = MatrixFactory.randn(rows, 1); 366 367 for (int r = 0; r < rows; r++) { 368 ret.setAsDouble((orig.getAsDouble(r, 0) * correlationFactor) 369 + ((1.0 - correlationFactor) * rand.getAsDouble(r, 0)), r, c); 370 } 371 } 372 373 return ret; 374 } 375 376 public static final DenseDoubleMatrix2D linkToValue(double value) { 377 return new ArrayDenseDoubleMatrix2D(new double[][] { { value } }); 378 } 379 380 public static final DenseIntMatrix2D linkToValue(int value) { 381 return new SimpleDenseIntMatrix2D(new int[][] { { value } }); 382 } 383 384 public static final DenseCharMatrix2D linkToValue(char value) { 385 return new ArrayDenseCharMatrix2D(new char[][] { { value } }); 386 } 387 388 public static final DenseDateMatrix2D linkToValue(Date value) { 389 return new SimpleDenseDateMatrix2D(new Date[][] { { value } }); 390 } 391 392 public static final DenseBooleanMatrix2D linkToValue(boolean value) { 393 return new DefaultDenseBooleanMatrix2D(new boolean[][] { { value } }); 394 } 395 396 public static final DenseByteMatrix2D linkToValue(byte value) { 397 return new ArrayDenseByteMatrix2D(new byte[][] { { value } }); 398 } 399 400 public static final DenseShortMatrix2D linkToValue(short value) { 401 return new SimpleDenseShortMatrix2D(new short[][] { { value } }); 402 } 403 404 public static final DenseStringMatrix2D linkToValue(String value) { 405 return new SimpleDenseStringMatrix2D(new String[][] { { value } }); 406 } 407 408 public static final DenseLongMatrix2D linkToValue(long value) { 409 return new SimpleDenseLongMatrix2D(new long[][] { { value } }); 410 } 411 412 public static final Matrix linkToValue(Object value) { 413 if (value == null) { 414 return emptyMatrix(); 415 } else if (value instanceof Double) { 416 return new ArrayDenseDoubleMatrix2D(new double[][] { { (Double) value } }); 417 } else if (value instanceof Integer) { 418 return new SimpleDenseIntMatrix2D(new int[][] { { (Integer) value } }); 419 } else if (value instanceof Float) { 420 return new ArrayDenseFloatMatrix2D(new float[][] { { (Float) value } }); 421 } else if (value instanceof String) { 422 return new SimpleDenseStringMatrix2D(new String[][] { { (String) value } }); 423 } else if (value instanceof Short) { 424 return new SimpleDenseShortMatrix2D(new short[][] { { (Short) value } }); 425 } else if (value instanceof Byte) { 426 return new ArrayDenseByteMatrix2D(new byte[][] { { (Byte) value } }); 427 } else if (value instanceof Boolean) { 428 return new DefaultDenseBooleanMatrix2D(new boolean[][] { { (Boolean) value } }); 429 } else if (value instanceof Long) { 430 return new SimpleDenseLongMatrix2D(new long[][] { { (Long) value } }); 431 } else { 432 return new SimpleDenseObjectMatrix2D(new Object[][] { { value } }); 433 } 434 } 435 436 public static Matrix zeros(ValueType valueType, long... size) throws MatrixException { 437 return dense(valueType, size); 438 } 439 440 public static Matrix dense(ValueType valueType, long... size) throws MatrixException { 441 if (ValueType.DOUBLE.equals(valueType)) { 442 return dense(size); 443 } 444 445 try { 446 Constructor<?> con = null; 447 448 switch (size.length) { 449 case 0: 450 throw new MatrixException("Size not defined"); 451 case 1: 452 throw new MatrixException("Size must be at least 2-dimensional"); 453 case 2: 454 switch (valueType) { 455 case BOOLEAN: 456 con = matrixMapper.getDenseBooleanMatrix2DConstructor(); 457 break; 458 case BYTE: 459 con = matrixMapper.getDenseByteMatrix2DConstructor(); 460 break; 461 case CHAR: 462 con = matrixMapper.getDenseCharMatrix2DConstructor(); 463 break; 464 case DATE: 465 con = matrixMapper.getDenseDateMatrix2DConstructor(); 466 break; 467 case FLOAT: 468 con = matrixMapper.getDenseFloatMatrix2DConstructor(); 469 break; 470 case INT: 471 con = matrixMapper.getDenseIntMatrix2DConstructor(); 472 break; 473 case LONG: 474 con = matrixMapper.getDenseLongMatrix2DConstructor(); 475 break; 476 case OBJECT: 477 con = matrixMapper.getDenseObjectMatrix2DConstructor(); 478 break; 479 case SHORT: 480 con = matrixMapper.getDenseShortMatrix2DConstructor(); 481 break; 482 case STRING: 483 con = matrixMapper.getDenseStringMatrix2DConstructor(); 484 break; 485 case BIGINTEGER: 486 con = matrixMapper.getDenseBigIntegerMatrix2DConstructor(); 487 break; 488 case BIGDECIMAL: 489 con = matrixMapper.getDenseBigDecimalMatrix2DConstructor(); 490 break; 491 default: 492 throw new MatrixException("entry type not yet supported: " + valueType); 493 } 494 break; 495 default: 496 switch (valueType) { 497 case BOOLEAN: 498 con = matrixMapper.getDenseBooleanMatrixMultiDConstructor(); 499 break; 500 case BYTE: 501 con = matrixMapper.getDenseByteMatrixMultiDConstructor(); 502 break; 503 case CHAR: 504 con = matrixMapper.getDenseCharMatrixMultiDConstructor(); 505 break; 506 case DATE: 507 con = matrixMapper.getDenseDateMatrixMultiDConstructor(); 508 break; 509 case FLOAT: 510 con = matrixMapper.getDenseFloatMatrixMultiDConstructor(); 511 break; 512 case INT: 513 con = matrixMapper.getDenseIntMatrixMultiDConstructor(); 514 break; 515 case LONG: 516 con = matrixMapper.getDenseLongMatrixMultiDConstructor(); 517 break; 518 case OBJECT: 519 con = matrixMapper.getDenseObjectMatrixMultiDConstructor(); 520 break; 521 case SHORT: 522 con = matrixMapper.getDenseShortMatrixMultiDConstructor(); 523 break; 524 case STRING: 525 con = matrixMapper.getDenseStringMatrixMultiDConstructor(); 526 break; 527 case BIGINTEGER: 528 con = matrixMapper.getDenseBigIntegerMatrixMultiDConstructor(); 529 break; 530 case BIGDECIMAL: 531 con = matrixMapper.getDenseBigDecimalMatrixMultiDConstructor(); 532 break; 533 default: 534 throw new MatrixException("entry type not yet supported: " + valueType); 535 } 536 } 537 538 return (Matrix) con.newInstance(size); 539 540 } catch (Exception e) { 541 throw new MatrixException(e); 542 } 543 544 } 545 546 /** 547 * @deprecated Please do not use this method anymore, it will be moved to 548 * <code>DenseMatrix.factory.ones</code> 549 */ 550 public static Matrix ones(long... size) throws MatrixException { 551 return Ones.calc(size); 552 } 553 554 public static Matrix fill(Object value, long... size) throws MatrixException { 555 return Fill.calc(value, size); 556 } 557 558 public static Matrix ones(ValueType valueType, long... size) throws MatrixException { 559 return Ones.calc(valueType, size); 560 } 561 562 /** 563 * @deprecated Please do not use this method anymore, it will be moved to 564 * <code>DenseMatrix.factory.eye</code> 565 */ 566 public static Matrix eye(long... size) throws MatrixException { 567 return Eye.calc(size); 568 } 569 570 public static Matrix magic(int size) throws MatrixException { 571 return Magic.magic(size); 572 } 573 574 public static Matrix pascal(long... size) throws MatrixException { 575 return new Pascal(zeros(ValueType.BIGINTEGER, size)).calcOrig(); 576 } 577 578 public static Matrix fibonacci(int count) throws MatrixException { 579 return Fibonacci.calc(count); 580 } 581 582 public static Matrix eye(ValueType valueType, long... size) throws MatrixException { 583 return Eye.calc(valueType, size); 584 } 585 586 public static final Matrix createVectorForClass(int classID, int classCount) 587 throws MatrixException { 588 Matrix matrix = MatrixFactory.dense(classCount, 1); 589 matrix.setAsDouble(1.0, classID, 0); 590 return matrix; 591 } 592 593 public static final FileListMatrix linkToDir(String dir) { 594 return new FileListMatrix(dir); 595 } 596 597 @SuppressWarnings("unchecked") 598 public static final Matrix linkToMap(Map<?, ?> map) { 599 if (map instanceof Matrix) { 600 return (Matrix) map; 601 } else { 602 return new DefaultMapMatrix(map); 603 } 604 } 605 606 public static final Matrix linkToCollection(Collection<?> list) { 607 if (list instanceof Matrix) { 608 return (Matrix) list; 609 } else { 610 return new DefaultListMatrix<Object>(list); 611 } 612 } 613 614 public static Matrix importFromStream(FileFormat format, InputStream stream, 615 Object... parameters) throws MatrixException, IOException { 616 return ImportMatrix.fromStream(format, stream, parameters); 617 } 618 619 public static Matrix importFromURL(FileFormat format, URL url, Object... parameters) 620 throws MatrixException, IOException { 621 return ImportMatrix.fromURL(format, url, parameters); 622 } 623 624 public static Matrix importFromURL(FileFormat format, String url, Object... parameters) 625 throws MatrixException, IOException { 626 return ImportMatrix.fromURL(format, url, parameters); 627 } 628 629 public static Matrix importFromString(FileFormat format, String string, Object... parameters) 630 throws MatrixException { 631 return ImportMatrix.fromString(format, string, parameters); 632 } 633 634 /** 635 * Wraps another Matrix so that all methods are executed synchronized. 636 * 637 * @param matrix 638 * the source Matrix 639 * @return a synchronized Matrix 640 */ 641 public static final <T> SynchronizedGenericMatrix<T> synchronizedMatrix(GenericMatrix<T> matrix) { 642 return new SynchronizedGenericMatrix<T>(matrix); 643 } 644 645 public static final DenseDoubleMatrix linkToBinaryFile(String filename, long... size) 646 throws IOException { 647 return new DenseFileMatrix(new File(filename), size); 648 } 649 650 public static final ObjectMatrix2D linkToJDBC(String url, String sqlStatement, String username, 651 String password) { 652 return LinkMatrixJDBC.toDatabase(url, sqlStatement, username, password); 653 } 654 655 public static final ObjectMatrix2D linkToJDBC(DB type, String host, int port, String database, 656 String sqlStatement, String username, String password) { 657 return LinkMatrixJDBC.toDatabase(type, host, port, database, sqlStatement, username, 658 password); 659 } 660 661 public static final ObjectMatrix2D importFromJDBC(String url, String sqlStatement, 662 String username, String password) { 663 return ImportMatrixJDBC.fromDatabase(url, sqlStatement, username, password); 664 } 665 666 public static final ObjectMatrix2D importFromJDBC(DB type, String host, int port, 667 String database, String sqlStatement, String username, String password) { 668 return ImportMatrixJDBC.fromDatabase(type, host, port, database, sqlStatement, username, 669 password); 670 } 671 672 /** 673 * @deprecated Please do not use this method anymore, it will be moved to 674 * <code>SparseMatrix.factory</code> 675 */ 676 public final static Matrix sparse(long... size) throws MatrixException { 677 return sparse(ValueType.DOUBLE, size); 678 } 679 680 public static final Matrix sparse(Matrix indices) { 681 return Dense2Sparse.calc(indices); 682 } 683 684 public final static Matrix sparse(ValueType valueType, long... size) throws MatrixException { 685 try { 686 Constructor<?> con = null; 687 688 switch (size.length) { 689 case 0: 690 throw new MatrixException("Size not defined"); 691 case 1: 692 throw new MatrixException("Size must be at least 2-dimensional"); 693 case 2: 694 switch (valueType) { 695 case BOOLEAN: 696 con = matrixMapper.getSparseBooleanMatrix2DConstructor(); 697 break; 698 case BYTE: 699 con = matrixMapper.getSparseByteMatrix2DConstructor(); 700 break; 701 case CHAR: 702 con = matrixMapper.getSparseCharMatrix2DConstructor(); 703 break; 704 case DATE: 705 con = matrixMapper.getSparseDateMatrix2DConstructor(); 706 break; 707 case DOUBLE: 708 con = matrixMapper.getSparseDoubleMatrix2DConstructor(); 709 break; 710 case FLOAT: 711 con = matrixMapper.getSparseFloatMatrix2DConstructor(); 712 break; 713 case INT: 714 con = matrixMapper.getSparseIntMatrix2DConstructor(); 715 break; 716 case LONG: 717 con = matrixMapper.getSparseLongMatrix2DConstructor(); 718 break; 719 case OBJECT: 720 con = matrixMapper.getSparseObjectMatrix2DConstructor(); 721 break; 722 case SHORT: 723 con = matrixMapper.getSparseShortMatrix2DConstructor(); 724 break; 725 case STRING: 726 con = matrixMapper.getSparseStringMatrix2DConstructor(); 727 break; 728 case BIGINTEGER: 729 con = matrixMapper.getSparseBigIntegerMatrix2DConstructor(); 730 break; 731 case BIGDECIMAL: 732 con = matrixMapper.getSparseBigDecimalMatrix2DConstructor(); 733 break; 734 default: 735 throw new MatrixException("entry type not supported: " + valueType); 736 } 737 break; 738 default: 739 switch (valueType) { 740 case BOOLEAN: 741 con = matrixMapper.getSparseBooleanMatrixMultiDConstructor(); 742 break; 743 case BYTE: 744 con = matrixMapper.getSparseByteMatrixMultiDConstructor(); 745 break; 746 case CHAR: 747 con = matrixMapper.getSparseCharMatrixMultiDConstructor(); 748 break; 749 case DATE: 750 con = matrixMapper.getSparseDateMatrixMultiDConstructor(); 751 break; 752 case DOUBLE: 753 con = matrixMapper.getSparseDoubleMatrixMultiDConstructor(); 754 break; 755 case FLOAT: 756 con = matrixMapper.getSparseFloatMatrixMultiDConstructor(); 757 break; 758 case INT: 759 con = matrixMapper.getSparseIntMatrixMultiDConstructor(); 760 break; 761 case LONG: 762 con = matrixMapper.getSparseLongMatrixMultiDConstructor(); 763 break; 764 case OBJECT: 765 con = matrixMapper.getSparseObjectMatrixMultiDConstructor(); 766 break; 767 case SHORT: 768 con = matrixMapper.getSparseShortMatrixMultiDConstructor(); 769 break; 770 case STRING: 771 con = matrixMapper.getSparseStringMatrixMultiDConstructor(); 772 break; 773 case BIGINTEGER: 774 con = matrixMapper.getSparseBigIntegerMatrixMultiDConstructor(); 775 break; 776 case BIGDECIMAL: 777 con = matrixMapper.getSparseBigDecimalMatrixMultiDConstructor(); 778 break; 779 default: 780 throw new MatrixException("entry type not supported: " + valueType); 781 } 782 } 783 784 return (Matrix) con.newInstance(size); 785 786 } catch (Exception e) { 787 throw new MatrixException(e); 788 } 789 } 790 791 /** 792 * @deprecated Please do not use this method anymore, it will be replaced by 793 * <code>DenseMatrix.factory.zeros(long... size)</code> 794 */ 795 public static Matrix zeros(long... size) throws MatrixException { 796 return dense(size); 797 } 798 799 /** 800 * @deprecated Please do not use this method anymore, it will be moved to 801 * <code>DenseMatrix.factory.zeros</code> 802 */ 803 public static Matrix dense(long... size) throws MatrixException { 804 return DenseMatrix.factory.zeros(size); 805 } 806 807 public static final Matrix linkToFile(FileFormat format, File file, Object... parameters) 808 throws MatrixException, IOException { 809 return LinkMatrix.toFile(format, file, parameters); 810 } 811 812 public static final Matrix importFromFile(String filename, Object... parameters) 813 throws MatrixException, IOException { 814 return ImportMatrix.fromFile(new File(filename), parameters); 815 } 816 817 public static final Matrix importFromFile(File file, Object... parameters) 818 throws MatrixException, IOException { 819 return ImportMatrix.fromFile(file, parameters); 820 } 821 822 public static final Matrix importFromFile(FileFormat format, String file, Object... parameters) 823 throws MatrixException, IOException { 824 return ImportMatrix.fromFile(format, new File(file), parameters); 825 } 826 827 public static final Matrix importFromFile(FileFormat format, File file, Object... parameters) 828 throws MatrixException, IOException { 829 return ImportMatrix.fromFile(format, file, parameters); 830 } 831 832 public static final Matrix importFromClipboard(FileFormat format, Object... parameters) 833 throws MatrixException { 834 return ImportMatrix.fromClipboard(format, parameters); 835 } 836 837 // Wolfer's sunspot data 1700 - 1987 838 public static final Matrix SUNSPOTDATASET() { 839 return MatrixFactory.linkToArray(new double[] { 5, 11, 16, 23, 36, 58, 29, 20, 10, 8, 3, 0, 840 0, 2, 11, 27, 47, 63, 60, 39, 28, 26, 22, 11, 21, 40, 78, 122, 103, 73, 47, 35, 11, 841 5, 16, 34, 70, 81, 111, 101, 73, 40, 20, 16, 5, 11, 22, 40, 60, 80.9, 83.4, 47.7, 842 47.8, 30.7, 12.2, 9.6, 10.2, 32.4, 47.6, 54, 62.9, 85.9, 61.2, 45.1, 36.4, 20.9, 843 11.4, 37.8, 69.8, 106.1, 100.8, 81.6, 66.5, 34.8, 30.6, 7, 19.8, 92.5, 154.4, 844 125.9, 84.8, 68.1, 38.5, 22.8, 10.2, 24.1, 82.9, 132, 130.9, 118.1, 89.9, 66.6, 60, 845 46.9, 41, 21.3, 16, 6.4, 4.1, 6.8, 14.5, 34, 45, 43.1, 47.5, 42.2, 28.1, 10.1, 8.1, 846 2.5, 0, 1.4, 5, 12.2, 13.9, 35.4, 45.8, 41.1, 30.1, 23.9, 15.6, 6.6, 4, 1.8, 8.5, 847 16.6, 36.3, 49.6, 64.2, 67, 70.9, 47.8, 27.5, 8.5, 13.2, 56.9, 121.5, 138.3, 103.2, 848 85.7, 64.6, 36.7, 24.2, 10.7, 15, 40.1, 61.5, 98.5, 124.7, 96.3, 66.6, 64.5, 54.1, 849 39, 20.6, 6.7, 4.3, 22.7, 54.8, 93.8, 95.8, 77.2, 59.1, 44, 47, 30.5, 16.3, 7.3, 850 37.6, 74, 139, 111.2, 101.6, 66.2, 44.7, 17, 11.3, 12.4, 3.4, 6, 32.3, 54.3, 59.7, 851 63.7, 63.5, 52.2, 25.4, 13.1, 6.8, 6.3, 7.1, 35.6, 73, 85.1, 78, 64, 41.8, 26.2, 852 26.7, 12.1, 9.5, 2.7, 5, 24.4, 42, 63.5, 53.8, 62, 48.5, 43.9, 18.6, 5.7, 3.6, 1.4, 853 9.6, 47.4, 57.1, 103.9, 80.6, 63.6, 37.6, 26.1, 14.2, 5.8, 16.7, 44.3, 63.9, 69, 854 77.8, 64.9, 35.7, 21.2, 11.1, 5.7, 8.7, 36.1, 79.7, 114.4, 109.6, 88.8, 67.8, 47.5, 855 30.6, 16.3, 9.6, 33.2, 92.6, 151.6, 136.3, 134.7, 83.9, 69.4, 31.5, 13.9, 4.4, 38, 856 141.7, 190.2, 184.8, 159, 112.3, 53.9, 37.5, 27.9, 10.2, 15.1, 47, 93.8, 105.9, 857 105.5, 104.5, 66.6, 68.9, 38, 34.5, 15.5, 12.6, 27.5, 92.5, 155.4, 154.6, 140.4, 858 115.9, 66.6, 45.9, 17.9, 13.4, 29.3 }); 859 } 860 861 public static final Matrix emptyMatrix() { 862 return EMPTYMATRIX; 863 } 864 865 public static MatrixMapper getMatrixMapper() { 866 return matrixMapper; 867 } 868 869 public static void setMatrixMapper(MatrixMapper matrixMapper) { 870 MatrixFactory.matrixMapper = matrixMapper; 871 } 872 873 public static Matrix repmat(Matrix matrix, long... count) { 874 return new Repmat(matrix, count).calc(Ret.LINK); 875 } 876 877 public static Matrix welcomeMatrix() { 878 return new WelcomeMatrix(); 879 } 880 881 public static Matrix like(Matrix matrix) { 882 if (matrix.isSparse()) { 883 return MatrixFactory.sparse(matrix.getSize()); 884 } else { 885 return MatrixFactory.dense(matrix.getSize()); 886 } 887 } 888 889 public static Matrix like(Matrix matrix, long... size) { 890 if (matrix.isSparse()) { 891 return SparseMatrix.factory.zeros(size); 892 } else { 893 return DenseMatrix.factory.zeros(size); 894 } 895 } 896 897 public static Matrix vertCat(Matrix row, long rowCount) { 898 Matrix[] matrices = new Matrix[(int) rowCount]; 899 Arrays.fill(matrices, row); 900 return vertCat(matrices); 901 } 902 903 public static Matrix horCat(Matrix column, long columnCount) { 904 Matrix[] matrices = new Matrix[(int) columnCount]; 905 Arrays.fill(matrices, column); 906 return horCat(matrices); 907 } 908 909 public static Matrix sequence(double start, double end) { 910 return sequence(start, end, 1); 911 } 912 913 public static Matrix sequence(double start, double end, double stepsize) { 914 return MatrixFactory.linkToArray(MathUtil.sequenceDouble(start, end, stepsize)); 915 } 916 917 }