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
037 package org.jblas.util;
038
039 import org.jblas.NativeBlas;
040 import org.jblas.DoubleMatrix;
041
042 /**
043 * Run a few sanity checks on the installation to see whether
044 * everything runs as expected.
045 *
046 * @author Mikio L. Braun
047 */
048 public class SanityChecks {
049
050 public static int checksFailed;
051
052 public static void check(String message, boolean condition) {
053 System.out.print(message + "... ");
054 if (condition) {
055 System.out.println("ok");
056 } else {
057 System.out.println("failed");
058 checksFailed++;
059 }
060 }
061
062 /** Check whether vector addition works. This is pure Java code and should work. */
063 public static void checkVectorAddition() {
064 DoubleMatrix x = new DoubleMatrix(3, 1, 1.0, 2.0, 3.0);
065 DoubleMatrix y = new DoubleMatrix(3, 1, 4.0, 5.0, 6.0);
066 DoubleMatrix z = new DoubleMatrix(3, 1, 5.0, 7.0, 9.0);
067
068 check("checking vector addition", x.add(y).equals(z));
069 }
070
071 /** Check matrix multiplication. This is already ATLAS/BLAS code. */
072 public static void checkMatrixMultiplication() {
073
074 DoubleMatrix A = new DoubleMatrix(new double[][]{
075 {1.0, 2.0, 3.0},
076 {4.0, 5.0, 6.0},
077 {7.0, 8.0, 9.0}
078 });
079 DoubleMatrix E = new DoubleMatrix(new double[][]{
080 {0.0, 0.0, 1.0},
081 {0.0, 1.0, 0.0},
082 {1.0, 0.0, 0.0}
083 });
084 DoubleMatrix B = new DoubleMatrix(new double[][]{
085 {3.0, 2.0, 1.0},
086 {6.0, 5.0, 4.0},
087 {9.0, 8.0, 7.0}
088 });
089
090 check("checking matrix multiplication", A.mmul(E).equals(B));
091 }
092
093 /**
094 * Check whether error handling works. If it works, you should see an
095 * ok, otherwise, you might see the actual error message and then
096 * the program exits.
097 */
098 public static void checkXerbla() {
099 double[] x = new double[9];
100 try {
101 NativeBlas.dgemm('N', 'N', 3, -1, 3, 1.0, x, 0, 3, x, 0, 3, 0.0, x, 0, 3);
102 } catch (IllegalArgumentException e) {
103 check("checking XERBLA", e.getMessage().contains("XERBLA"));
104 return;
105 }
106 assert (false); // shouldn't happen
107 }
108
109 /**
110 * Compute eigenvalues. This is a routine not in ATLAS, but in the original
111 * LAPACK.
112 */
113 public static void checkEigenvalues() {
114 DoubleMatrix A = new DoubleMatrix(new double[][]{
115 {3.0, 2.0, 0.0},
116 {2.0, 3.0, 2.0},
117 {0.0, 2.0, 3.0}
118 });
119
120 DoubleMatrix E = new DoubleMatrix(3, 1);
121
122 NativeBlas.dsyev('N', 'U', 3, A.data, 0, 3, E.data, 0);
123 check("checking existence of dsyev...", true);
124 }
125
126 public static void main(String[] args) {
127 Logger.getLogger().setLevel(Logger.CONFIG);
128 for (String arg: args) {
129 if (arg.equals("--debug")) {
130 Logger.getLogger().setLevel(Logger.DEBUG);
131 }
132 }
133 checkVectorAddition();
134 checkMatrixMultiplication();
135 checkEigenvalues();
136 checkXerbla();
137 printSummary();
138 }
139
140 private static void printSummary() {
141 if (checksFailed == 0) {
142 System.out.println("Sanity checks passed.");
143 } else {
144 System.out.println("Sainty checks FAILED!");
145 }
146 }
147 }