File: TestMatrixFeatures_DSCC.java

package info (click to toggle)
libejml-java 0.41%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 7,376 kB
  • sloc: java: 82,734; python: 81; makefile: 22
file content (338 lines) | stat: -rw-r--r-- 12,580 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * Copyright (c) 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;

import org.ejml.UtilEjml;
import org.ejml.data.DMatrixRMaj;
import org.ejml.data.DMatrixSparseCSC;
import org.ejml.data.DMatrixSparseTriplet;
import org.ejml.dense.row.CommonOps_DDRM;
import org.ejml.dense.row.RandomMatrices_DDRM;
import org.ejml.ops.DConvertMatrixStruct;
import org.junit.jupiter.api.Test;

import java.util.Random;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author Peter Abeles
 */
public class TestMatrixFeatures_DSCC {

    Random rand = new Random(234);

    @Test
    public void isEquals() {
        DMatrixSparseTriplet orig = new DMatrixSparseTriplet(4,5,3);
        orig.addItem(3,1,2.5);
        orig.addItem(2,4,2.7);
        orig.addItem(2,2,1.5);

        DMatrixSparseCSC a = DConvertMatrixStruct.convert(orig,(DMatrixSparseCSC)null);
        DMatrixSparseCSC b = DConvertMatrixStruct.convert(orig,(DMatrixSparseCSC)null);

        a.sortIndices(null); b.sortIndices(null);
        assertTrue(MatrixFeatures_DSCC.isEquals(a,b));

        b.numRows += 1;
        assertFalse(MatrixFeatures_DSCC.isEquals(a,b));
        b.numRows -= 1; b.numCols += 1;
        assertFalse(MatrixFeatures_DSCC.isEquals(a,b));
        b.numCols -= 1; b.nz_rows[1]++;
        assertFalse(MatrixFeatures_DSCC.isEquals(a,b));
        b.nz_rows[1]--; b.col_idx[1]++;
        assertFalse(MatrixFeatures_DSCC.isEquals(a,b));
        b.col_idx[1]--;

        // make it no longer exactly equal
        b.nz_values[0] += UtilEjml.TEST_F64*0.1;
        assertFalse(MatrixFeatures_DSCC.isEquals(a,b));
    }

    @Test
    public void isEqualsSort_tol() {
        DMatrixSparseTriplet orig = new DMatrixSparseTriplet(4,5,3);
        orig.addItem(3,1,2.5);
        orig.addItem(2,4,2.7);
        orig.addItem(2,2,1.5);

        DMatrixSparseCSC a = DConvertMatrixStruct.convert(orig,(DMatrixSparseCSC)null);

        orig = new DMatrixSparseTriplet(4,5,3);
        orig.addItem(3,1,2.5);
        orig.addItem(2,2,1.5);
        orig.addItem(2,4,2.7);
        DMatrixSparseCSC b = DConvertMatrixStruct.convert(orig,(DMatrixSparseCSC)null);

        // these require sorting and this will fail if not
        assertTrue(MatrixFeatures_DSCC.isEqualsSort(a,b, UtilEjml.TEST_F64));
    }

    @Test
    public void isIdenticalSort_tol() {
        DMatrixSparseTriplet orig = new DMatrixSparseTriplet(4,5,3);
        orig.addItem(3,1,2.5);
        orig.addItem(2,4,2.7);
        orig.addItem(2,2,1.5);

        DMatrixSparseCSC a = DConvertMatrixStruct.convert(orig,(DMatrixSparseCSC)null);

        orig = new DMatrixSparseTriplet(4,5,3);
        orig.addItem(3,1,2.5);
        orig.addItem(2,2,1.5);
        orig.addItem(2,4,2.7);
        DMatrixSparseCSC b = DConvertMatrixStruct.convert(orig,(DMatrixSparseCSC)null);

        // these require sorting and this will fail if not
        assertTrue(MatrixFeatures_DSCC.isIdenticalSort(a,b, UtilEjml.TEST_F64));

        double values[] = new double[]{1.0,Double.NaN,Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY};

        for( int i = 0; i < values.length; i++ ) {
            for( int j = 0; j < values.length; j++ ) {
                a.set(2,2,values[i]);
                b.set(2,2,values[j]);
                assertEquals(i==j,MatrixFeatures_DSCC.isIdenticalSort(a,b, UtilEjml.TEST_F64));
            }
        }
    }

    @Test
    public void isEquals_tol() {
        DMatrixSparseTriplet orig = new DMatrixSparseTriplet(4,5,3);
        orig.addItem(3,1,2.5);
        orig.addItem(2,4,2.7);
        orig.addItem(2,2,1.5);

        DMatrixSparseCSC a = DConvertMatrixStruct.convert(orig,(DMatrixSparseCSC)null);
        DMatrixSparseCSC b = DConvertMatrixStruct.convert(orig,(DMatrixSparseCSC)null);

        assertTrue(MatrixFeatures_DSCC.isEqualsSort(a,b,UtilEjml.TEST_F64));

        b.numRows += 1;
        assertFalse(MatrixFeatures_DSCC.isEqualsSort(a,b,UtilEjml.TEST_F64));
        b.numRows -= 1; b.numCols += 1;
        assertFalse(MatrixFeatures_DSCC.isEqualsSort(a,b,UtilEjml.TEST_F64));
        b.numCols -= 1; b.nz_rows[1]++;
        assertFalse(MatrixFeatures_DSCC.isEqualsSort(a,b,UtilEjml.TEST_F64));
        b.nz_rows[1]--; b.col_idx[1]++;
        assertFalse(MatrixFeatures_DSCC.isEqualsSort(a,b,UtilEjml.TEST_F64));
        b.col_idx[1]--;

        // make it no longer exactly equal, but within tolerance
        b.nz_values[0] += UtilEjml.TEST_F64*0.1;
        assertTrue(MatrixFeatures_DSCC.isEqualsSort(a,b,UtilEjml.TEST_F64));

        // outside of tolerance
        b.nz_values[0] += UtilEjml.TEST_F64*10;
        assertFalse(MatrixFeatures_DSCC.isEqualsSort(a,b,UtilEjml.TEST_F64));
    }

    @Test
    public void hasUncountable() {
        for( int length : new int[]{0,2,6,15,30} ) {
            DMatrixSparseCSC A = RandomMatrices_DSCC.rectangle(6,6,length,rand);

            assertFalse(MatrixFeatures_DSCC.hasUncountable(A));

            if( length == 0 )
                continue;
            int selected = rand.nextInt(length);

            A.nz_values[selected] = Double.NaN;
            assertTrue(MatrixFeatures_DSCC.hasUncountable(A));

            A.nz_values[selected] = Double.POSITIVE_INFINITY;
            assertTrue(MatrixFeatures_DSCC.hasUncountable(A));

            A.nz_values[selected] = Double.NEGATIVE_INFINITY;
            assertTrue(MatrixFeatures_DSCC.hasUncountable(A));
        }
    }

    @Test
    public void isZeros() {
        assertTrue( MatrixFeatures_DSCC.isZeros(new DMatrixSparseCSC(10,12,0), UtilEjml.TEST_F64));

        DMatrixSparseCSC A = RandomMatrices_DSCC.rectangle(6,4,12,rand);
        for (int i = 0; i < A.nz_length; i++) {
            A.nz_values[i] = UtilEjml.EPS;
        }

        assertTrue( MatrixFeatures_DSCC.isZeros(A, UtilEjml.TEST_F64));
    }

    @Test
    public void isIdentity() {
        for( int length : new int[]{1,2,6,15,30} ) {
            DMatrixSparseCSC A = CommonOps_DSCC.identity(length);

            assertTrue(MatrixFeatures_DSCC.isIdentity(A, UtilEjml.TEST_F64));

            A.nz_values[0] = 1 + 2.0*UtilEjml.EPS;
            assertTrue(MatrixFeatures_DSCC.isIdentity(A, UtilEjml.TEST_F64));

            A.nz_values[0] = 1.1;
            assertFalse(MatrixFeatures_DSCC.isIdentity(A, UtilEjml.TEST_F64));
        }
    }

    @Test
    public void isLowerTriangle() {
        // normal triangular matrix
        DMatrixRMaj D = new DMatrixRMaj(4,4,true,
                1,0,0,0, 1,1,0,0, 0,0,1,0 , 1,0,1,1 );
        DMatrixSparseCSC L = DConvertMatrixStruct.convert(D,(DMatrixSparseCSC)null, UtilEjml.EPS);

        assertTrue(MatrixFeatures_DSCC.isLowerTriangle(L,0, UtilEjml.TEST_F64));
        assertFalse(MatrixFeatures_DSCC.isLowerTriangle(L,1, UtilEjml.TEST_F64));
        L.nz_values[L.nz_length -1] = UtilEjml.EPS;
        assertFalse(MatrixFeatures_DSCC.isLowerTriangle(L,0, UtilEjml.TEST_F64));

        // Hessenberg matrix of degree 1
        D = new DMatrixRMaj(4,4,true,
                1,1,0,0, 1,1,1,0, 0,0,0,1 , 1,0,1,1 );
        L = DConvertMatrixStruct.convert(D,(DMatrixSparseCSC)null, UtilEjml.EPS);

        assertFalse(MatrixFeatures_DSCC.isLowerTriangle(L,0, UtilEjml.TEST_F64));
        assertTrue(MatrixFeatures_DSCC.isLowerTriangle(L,1, UtilEjml.TEST_F64));
        assertFalse(MatrixFeatures_DSCC.isLowerTriangle(L,2, UtilEjml.TEST_F64));
        L.set(0,1,UtilEjml.EPS);
        assertFalse(MatrixFeatures_DSCC.isLowerTriangle(L,1, UtilEjml.TEST_F64));

        // testing a case which failed.  first column was all zeros for hessenberg of 1
        D = new DMatrixRMaj(4,4,true,
                0,1,0,0, 0,0,1,0, 0,0,0,1 , 0,0,0,1 );
        L = DConvertMatrixStruct.convert(D,(DMatrixSparseCSC)null, UtilEjml.EPS);

        assertFalse(MatrixFeatures_DSCC.isLowerTriangle(L,0, UtilEjml.TEST_F64));
        assertTrue(MatrixFeatures_DSCC.isLowerTriangle(L,1, UtilEjml.TEST_F64));
        assertFalse(MatrixFeatures_DSCC.isLowerTriangle(L,2, UtilEjml.TEST_F64));
        L.set(0,1,1);
        assertTrue(MatrixFeatures_DSCC.isLowerTriangle(L,1, UtilEjml.TEST_F64));
    }

    @Test
    public void isTranspose() {

        // do it first in dense matrices
        DMatrixRMaj Ad = RandomMatrices_DDRM.rectangle(5,4,-1,1,rand);
        Ad.data[5] = 0; Ad.data[10] = 0; Ad.data[11] = 0;

        DMatrixRMaj Ad_tran = new DMatrixRMaj(4,5);
        CommonOps_DDRM.transpose(Ad,Ad_tran);

        // convert to sparse
        DMatrixSparseCSC A = DConvertMatrixStruct.convert(Ad,(DMatrixSparseCSC)null, UtilEjml.EPS);
        DMatrixSparseCSC At = DConvertMatrixStruct.convert(Ad_tran,(DMatrixSparseCSC)null, UtilEjml.EPS);

        A.sortIndices(null);
        assertTrue(MatrixFeatures_DSCC.isTranspose(A,At, UtilEjml.TEST_F64));
        At.nz_values[4] += 0.01;
        assertFalse(MatrixFeatures_DSCC.isTranspose(A,At, UtilEjml.TEST_F64));

    }

    @Test
    public void isVector() {
        assertTrue(MatrixFeatures_DSCC.isVector(new DMatrixSparseCSC(10,1,5)));
        assertTrue(MatrixFeatures_DSCC.isVector(new DMatrixSparseCSC(1,10,5)));
        assertFalse(MatrixFeatures_DSCC.isVector(new DMatrixSparseCSC(10,10,5)));
        assertFalse(MatrixFeatures_DSCC.isVector(new DMatrixSparseCSC(1,1,5)));
    }

    @Test
    public void isSymmetric() {
        DMatrixSparseCSC A = new DMatrixSparseCSC(5,4,2);

        // not square
        assertFalse(MatrixFeatures_DSCC.isSymmetric(A,UtilEjml.TEST_F64));

        // a matrix with all zeros is symmetric
        A.reshape(5,5,2);
        assertTrue(MatrixFeatures_DSCC.isSymmetric(A,UtilEjml.TEST_F64));

        // test with various random matrices
        for (int N = 1; N < 10; N++) {
            for (int mc = 0; mc < 30; mc++) {
                int nz = (int)Math.ceil(N*N*(rand.nextDouble()*0.4+0.1));
                A = RandomMatrices_DSCC.rectangle(N,N,nz,rand);
                DMatrixSparseCSC A_t = CommonOps_DSCC.transpose(A,null,null);

                DMatrixSparseCSC C = new DMatrixSparseCSC(N,N,0);

                // C must be symmetric
                CommonOps_DSCC.mult(A,A_t,C,null,null);
                assertTrue(MatrixFeatures_DSCC.isSymmetric(C,UtilEjml.TEST_F64));

                // make it not symmetric
                if( N > 1 ) {
                    int index = rand.nextInt(C.nz_length);
                    C.nz_values[index] += 0.1;

                    // see if it modified a diagonal element. still will be diagonal if it did
                    int row =  C.nz_rows[index];
                    int col = 0;
                    while( col < C.numCols ) {
                        if( index < C.col_idx[col+1] )
                            break;
                        col++;
                    }
                    if( row != col )
                        assertFalse(MatrixFeatures_DSCC.isSymmetric(C, UtilEjml.TEST_F64));
                }
            }
        }
    }

    @Test
    public void isPositiveDefinite() {
        DMatrixSparseCSC a = UtilEjml.parse_DSCC("2 0 0 2",2);
        DMatrixSparseCSC b = UtilEjml.parse_DSCC("0 1 1 0",2);
        DMatrixSparseCSC c = UtilEjml.parse_DSCC("0 0 0 0",2);

        assertTrue(MatrixFeatures_DSCC.isPositiveDefinite(a));
        assertFalse(MatrixFeatures_DSCC.isPositiveDefinite(b));
        assertFalse(MatrixFeatures_DSCC.isPositiveDefinite(c));
    }

    @Test
    public void isOrthogonal() {
        // rotation matrices are orthogonal
        double c = Math.cos(0.1);
        double s = Math.sin(0.1);

        DMatrixSparseCSC A = new DMatrixSparseCSC(2,2,2);
        A.set(0,0,c);A.set(0,1,s);
        A.set(1,0,-s);A.set(1,1,c);

        assertTrue(MatrixFeatures_DSCC.isOrthogonal(A,UtilEjml.TEST_F64_SQ));

        // try a negative case now
        A.set(0,1,495);

        assertFalse(MatrixFeatures_DSCC.isOrthogonal(A,UtilEjml.TEST_F64_SQ));

        A.set(0,1,Double.NaN);

        assertFalse(MatrixFeatures_DSCC.isOrthogonal(A,UtilEjml.TEST_F64_SQ));
    }

}