File: CheckMatrixMultShape_DSCC.java

package info (click to toggle)
libejml-java 0.41%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,376 kB
  • sloc: java: 82,734; python: 81; makefile: 22
file content (276 lines) | stat: -rw-r--r-- 9,553 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (c) 2009-2020, Peter Abeles. All Rights Reserved.
 *
 * This file is part of Efficient Java Matrix Library (EJML).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ejml.sparse.csc.mult;

import org.ejml.MatrixDimensionException;
import org.ejml.data.DMatrix;
import org.ejml.data.DMatrixRMaj;
import org.ejml.data.DMatrixSparseCSC;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static org.ejml.UtilEjml.hasNullableArgument;
import static org.junit.jupiter.api.Assertions.*;


/**
 * Checks to see if the input to a matrix mutiply is accepted or rejected correctly depending
 * on the shape in the input matrices.  Java reflections is used to grab all functions
 * with "mult" in its name and then it determins if any of matrices are transposed.
 *
 * @author Peter Abeles
 */
@SuppressWarnings("rawtypes")
public class CheckMatrixMultShape_DSCC {

    // TODO merge with CheckMatrixMultShape_DDRM - Need to mess with project dependencies to do that

    Class theClass;

    public CheckMatrixMultShape_DSCC(Class theClass ) {
        this.theClass = theClass;
    }

    /**
     * Perform all shape input checks.
     */
    public void checkAll()
    {
        int numChecked = 0;
        Method[] methods = theClass.getMethods();

        for( Method method : methods ) {
            String name = method.getName();

            // only look at function which perform matrix multiplcation
            if( !name.contains("mult") || name.contains("Element") ||
                    name.contains("Inner") || name.contains("Outer") )
                continue;
            if( name.equals("multRows") || name.equals("multCols") || name.equals("multColumns") || name.equals("multRowsCols"))
                continue;

            boolean transA = false;
            boolean transB = false;

            if( name.contains("TransAB")) {
                transA = true;
                transB = true;
            } else if( name.contains("TransA")) {
                transA = true;
            } else if( name.contains("TransB")) {
                transB = true;
            }

            try {
                checkPositive(method, transA, transB);
                checkNegative(method, transA, transB);
            } catch( Throwable e ) {
                System.out.println("Failed on "+name);
                e.printStackTrace();
                fail("An exception was thrown");
            }
            numChecked++;
        }

        // make sure some functions were checked!
        assertTrue(numChecked!=0);
    }

    /**
     * Iterate through a variety of different sizes and shapes of matrices.
     */
    private void checkPositive( Method func, boolean transA, boolean transB )
            throws InvocationTargetException, IllegalAccessException {
        for( int i = 1; i <= 4; i++ ) {
            for( int j = 1; j <= 4; j++ ) {
                for( int k = 1; k <= 4; k++ ) {
                    checkPositive(func,transA,transB,i,j,k);
                }
            }
        }
    }

    /**
     * See if the function can be called with matrices of the correct size
     */
    private void checkPositive(Method func, boolean transA, boolean transB ,
                               int m , int n , int o ) throws IllegalAccessException, InvocationTargetException {
        DMatrix A,B;
        DMatrix C = createMatrix(getMatrixType(func,2),m,o);

        if( transA ) {
            A = createMatrix(getMatrixType(func,0),n,m);
        } else {
            A = createMatrix(getMatrixType(func,0),m,n);
        }
        if( transB ) {
            B = createMatrix(getMatrixType(func,1),o,n);
        } else {
            B = createMatrix(getMatrixType(func,1),n,o);
        }

        invoke(func, 2.0, A, B, C);

        if( hasNullableArgument(func) ) {
            DMatrix ret = invoke(func, 2.0, A, B, null);
            assertNotNull(ret);
            assertEquals(ret.getNumRows(),C.getNumRows());
            assertEquals(ret.getNumCols(),C.getNumCols());
        }
    }

    /**
     * See if the function throws an exception when it is given bad inputs
     */
    private void checkNegative(Method func, boolean transA, boolean transB) throws IllegalAccessException {
        // don't reshape if it adds since C is also an input
        boolean reshape = !func.getName().contains("Add");

//        System.out.println("func = "+func);
        // correct = 2,4,4,3,2,3
        //           i,j,j,k,i,k

        // mis matched i
        if( reshape )
            checkReshapeC(func,2,4,4,3,6,3,transA,transB);
        else
            checkNegative(func,2,4,5,3,2,3,transA,transB);
        // missmatched j
        checkNegative(func,2,4,5,3,2,3,transA,transB);
        // miss matched k
        if( reshape )
            checkReshapeC(func,2,4,4,7,2,3,transA,transB);
        else
            checkNegative(func,2,4,4,7,2,3,transA,transB);
    }

    /**
     * See if the function throws an exception when it is given bad inputs
     */
    private void checkNegative(Method func,
                               int m_a , int n_a , int m_b , int n_b , int m_c , int n_c ,
                               boolean transA, boolean transB) throws IllegalAccessException {
        DMatrix A,B;
        DMatrix C = createMatrix(getMatrixType(func,2),m_c,n_c);

        if( transA ) {
            A = createMatrix(getMatrixType(func,0),n_a,m_a);
        } else {
            A = createMatrix(getMatrixType(func,0),m_a,n_a);
        }
        if( transB ) {
            B = createMatrix(getMatrixType(func,1),n_b,m_b);
        } else {
            B = createMatrix(getMatrixType(func,1),m_b,n_b);
        }
        try {
            invoke(func, 2.0, A, B, C);
            fail("An exception should have been thrown. name="+func.getName());
        } catch( InvocationTargetException e ) {
            assertSame(e.getCause().getClass(), MatrixDimensionException.class);
        }
    }

    /**
     * The C matrix will have the incorrect size, see if it's reshaped correctly
     */
    private void checkReshapeC(Method func,
                               int m_a , int n_a , int m_b , int n_b , int m_c , int n_c ,
                               boolean transA, boolean transB) throws IllegalAccessException {
        DMatrix A,B;
        DMatrix C = createMatrix(getMatrixType(func,2),m_c,n_c);

        if( transA ) {
            A = createMatrix(getMatrixType(func,0),n_a,m_a);
        } else {
            A = createMatrix(getMatrixType(func,0),m_a,n_a);
        }
        if( transB ) {
            B = createMatrix(getMatrixType(func,1),n_b,m_b);
        } else {
            B = createMatrix(getMatrixType(func,1),m_b,n_b);
        }

        try {
            invoke(func, 2.0, A, B, C);
            assertEquals(m_a,C.getNumRows());
            assertEquals(n_b,C.getNumCols());
        } catch( InvocationTargetException e ) {
            e.printStackTrace();
            fail("there should be no exception! ("+A.getNumRows()+"x"+A.getNumCols()+") ("+B.getNumRows()+"x"+B.getNumCols()+")");
        }
    }

    public static Class getMatrixType( Method func , int index ) {
        Class<?>[] parameters = func.getParameterTypes();
        if( parameters[0] == double.class ) {
            return parameters[index+1];
        } else {
            return parameters[index];
        }
    }

    public static @Nullable DMatrix invoke(Method func,
                                           double alpha,
                                           DMatrix a, DMatrix b, @Nullable DMatrix c)
            throws IllegalAccessException, InvocationTargetException {

        Object[] arguments = new Object[func.getParameterTypes().length];
        Object ret;
        if( func.getParameterTypes().length == 3 ) {
            arguments[0] = a;
            arguments[1] = b;
            arguments[2] = c;
        } else {
            if( func.getParameterTypes()[0] == double.class ) {
                arguments[0] = alpha;
                arguments[1] = a;
                arguments[2] = b;
                arguments[3] = c;
            } else {
                arguments[0] = a;
                arguments[1] = b;
                arguments[2] = c;
            }
        }

        try {
            ret = func.invoke(null, arguments);
        } catch( IllegalArgumentException e ) {
            for( Class pc : func.getParameterTypes() ) {
                System.out.println(pc.getName());
            }
            throw e;
        }

        if( ret instanceof DMatrix )
            return (DMatrix)ret;
        return null;
    }

    public static DMatrix createMatrix( Class type , int rows , int cols ) {
        if( type == DMatrixSparseCSC.class )
            return new DMatrixSparseCSC(rows,cols);
        else if( type == DMatrixRMaj.class )
            return new DMatrixRMaj(rows,cols);
        throw new RuntimeException("Unknown matrix type: "+type.getSimpleName());
    }
}