001    /*
002     * To change this template, choose Tools | Templates
003     * and open the template in the editor.
004     */
005    package org.jblas.util;
006    
007    import org.jblas.NativeBlas;
008    import org.jblas.DoubleMatrix;
009    
010    /**
011     *
012     */
013    public class SanityChecks {
014    
015        public static int checksFailed;
016    
017        public static void check(String message, boolean condition) {
018            System.out.print(message + "... ");
019            if (condition) {
020                System.out.println("ok");
021            } else {
022                System.out.println("failed");
023                checksFailed++;
024            }
025        }
026    
027        public static void checkVectorAddition() {
028            DoubleMatrix x = new DoubleMatrix(3, 1, 1.0, 2.0, 3.0);
029            DoubleMatrix y = new DoubleMatrix(3, 1, 4.0, 5.0, 6.0);
030            DoubleMatrix z = new DoubleMatrix(3, 1, 5.0, 7.0, 9.0);
031    
032            check("checking vector addition", x.add(y).equals(z));
033        }
034    
035        public static void checkMatrixMultiplication() {
036    
037            DoubleMatrix A = new DoubleMatrix(new double[][] {
038                { 1.0, 2.0, 3.0 },
039                { 4.0, 5.0, 6.0 },
040                { 7.0, 8.0, 9.0 }
041            });
042            DoubleMatrix E = new DoubleMatrix(new double[][] {
043                { 0.0, 0.0, 1.0 },
044                { 0.0, 1.0, 0.0 },
045                { 1.0, 0.0, 0.0 }
046            });
047            DoubleMatrix B = new DoubleMatrix(new double[][] {
048                { 3.0, 2.0, 1.0 },
049                { 6.0, 5.0, 4.0 },
050                { 9.0, 8.0, 7.0 }
051            });
052    
053            check("checking matrix multiplication", A.mmul(E).equals(B));
054        }
055    
056        public static void checkXerbla() {
057            double[] x = new double[9];
058            try {
059                NativeBlas.dgemm('N', 'N', 3, -1, 3, 1.0, x, 0, 3, x, 0, 3, 0.0, x, 0, 3);
060            }
061            catch(IllegalArgumentException e) {
062                check("checking XERBLA", e.getMessage().contains("XERBLA"));
063                return;
064            }
065            assert(false); // shouldn't happen
066        }
067    
068        public static void main(String[] args) {
069            checkVectorAddition();
070            checkMatrixMultiplication();
071            checkXerbla();
072            printSummary();
073        }
074    
075        private static void printSummary() {
076            if (checksFailed == 0) {
077                System.out.println("Sanity checks passed.");
078            }
079            else {
080                System.out.println("Sainty checks FAILED!");
081            }
082        }
083    }