File: TestImplCommonOps_DSCC.java

package info (click to toggle)
libejml-java 0.38%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,336 kB
  • sloc: java: 73,523; python: 81; xml: 60; makefile: 58
file content (232 lines) | stat: -rw-r--r-- 8,393 bytes parent folder | download
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
/*
 * Copyright (c) 2009-2018, 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.misc;

import org.ejml.EjmlUnitTests;
import org.ejml.UtilEjml;
import org.ejml.data.DMatrixSparseCSC;
import org.ejml.data.IGrowArray;
import org.ejml.sparse.csc.CommonOps_DSCC;
import org.ejml.sparse.csc.MatrixFeatures_DSCC;
import org.ejml.sparse.csc.RandomMatrices_DSCC;
import org.junit.Test;

import java.util.Random;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

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

    private Random rand = new Random(324);

    @Test
    public void transpose() {
        for (int rows = 1; rows <= 10; rows++) {
            for (int cols = 1; rows <= 10; rows++) {
                for (int mc = 0; mc < 20; mc++) {
                    int N =(int) Math.round(rows*cols*rand.nextDouble());
                    DMatrixSparseCSC a = RandomMatrices_DSCC.rectangle(rows,cols,N, -1, 1, rand);
                    DMatrixSparseCSC b = new DMatrixSparseCSC(0,0,0);

                    ImplCommonOps_DSCC.transpose(a,b,null);
                    assertEquals(cols,b.numRows);
                    assertEquals(rows,b.numCols);

                    for (int row = 0; row < rows; row++) {
                        for (int col = 0; col < cols; col++) {
                            double expected = a.get(row,col);
                            double found = b.get(col,row);

                            assertEquals(row+" "+col,expected, found, UtilEjml.TEST_F64);
                        }
                    }
                    assertTrue(CommonOps_DSCC.checkSortedFlag(b));
                }
            }
        }
    }

    @Test
    public void add() {
        double alpha = 1.5;
        double beta = 2.3;

        for( int numRows : new int[]{2,4,6,10}) {
            for( int numCols : new int[]{2,4,6,10}) {
                DMatrixSparseCSC a = RandomMatrices_DSCC.rectangle(numRows,numCols,7, -1, 1, rand);
                DMatrixSparseCSC b = RandomMatrices_DSCC.rectangle(numRows,numCols,8, -1, 1, rand);
                DMatrixSparseCSC c = RandomMatrices_DSCC.rectangle(numRows,numCols,3, -1, 1, rand);

                ImplCommonOps_DSCC.add(alpha,a,beta,b,c, null, null);

                for (int row = 0; row < numRows; row++) {
                    for (int col = 0; col < numCols; col++) {
                        double valA = a.get(row,col);
                        double valB = b.get(row,col);
                        double found = alpha*valA + beta*valB;

                        double expected = c.get(row,col);

                        assertEquals(row+" "+col,expected, found, UtilEjml.TEST_F64);
                    }
                }
                assertTrue(CommonOps_DSCC.checkStructure(c));
            }
        }
    }

    @Test
    public void addColAppend() {
        double alpha = 1.5;
        double beta = 2.3;

        for( int numRows : new int[]{2,4,6,10}) {
            DMatrixSparseCSC a = RandomMatrices_DSCC.rectangle(numRows,3,15, -1, 1, rand);
            DMatrixSparseCSC b = RandomMatrices_DSCC.rectangle(numRows,4,15, -1, 1, rand);
            DMatrixSparseCSC c = new DMatrixSparseCSC(numRows,0,0);


            ImplCommonOps_DSCC.addColAppend(alpha,a,1,beta,b,0,c, null);
            assertTrue(CommonOps_DSCC.checkStructure(c));
            assertEquals(1,c.numCols);

            for (int row = 0; row < numRows; row++) {
                double valA = a.get(row,1);
                double valB = b.get(row,0);
                double found = alpha*valA + beta*valB;

                double expected = c.get(row,0);
                assertEquals(expected, found, UtilEjml.TEST_F64);
            }

            ImplCommonOps_DSCC.addColAppend(alpha,a,2,beta,b,1,c, null);
            assertTrue(CommonOps_DSCC.checkStructure(c));
            assertEquals(2,c.numCols);

            for (int row = 0; row < numRows; row++) {
                double valA = a.get(row,2);
                double valB = b.get(row,1);
                double found = alpha*valA + beta*valB;

                double expected = c.get(row,1);
                assertEquals(expected, found, UtilEjml.TEST_F64);
            }
        }
    }

    @Test
    public void removeZeros_two() {
        DMatrixSparseCSC A = new DMatrixSparseCSC(2,3,2);
        A.indicesSorted = true;
        A.set(0,1,0.1);
        A.set(1,1,0.0);
        A.set(1,2,0.3);
        DMatrixSparseCSC A_orig = A.copy();

        DMatrixSparseCSC B = new DMatrixSparseCSC(1,1,1);

        ImplCommonOps_DSCC.removeZeros(A,B,0);
        assertTrue(CommonOps_DSCC.checkStructure(B));
        assertEquals(A.numRows,B.numRows);
        assertEquals(A.numCols,B.numCols);
        assertEquals(2,B.nz_length);
        EjmlUnitTests.assertEquals(A,B,UtilEjml.TEST_F64);
        assertTrue(MatrixFeatures_DSCC.isEquals(A,A_orig,UtilEjml.TEST_F64));

        ImplCommonOps_DSCC.removeZeros(A,B,0.1);
        assertEquals(1,B.nz_length);
        assertEquals(0,B.get(0,1), UtilEjml.TEST_F64);
        assertEquals(A.get(1,1),B.get(1,1),UtilEjml.TEST_F64);
        assertTrue(MatrixFeatures_DSCC.isEquals(A,A_orig,UtilEjml.TEST_F64));
    }

    @Test
    public void removeZeros_one() {
        DMatrixSparseCSC A = new DMatrixSparseCSC(2,3,2);
        A.set(0,1,0.1);
        A.set(1,1,0.0);
        A.set(1,2,0.3);

        ImplCommonOps_DSCC.removeZeros(A,0);
        assertTrue(CommonOps_DSCC.checkStructure(A));
        assertEquals(A.numRows,2);
        assertEquals(A.numCols,3);
        assertEquals(2,A.nz_length);
        assertEquals(0.1,A.get(0,1),UtilEjml.TEST_F64);
        assertEquals(0.3,A.get(1,2),UtilEjml.TEST_F64);

        ImplCommonOps_DSCC.removeZeros(A,0.1);
        assertTrue(CommonOps_DSCC.checkStructure(A));
        assertEquals(A.numRows,2);
        assertEquals(A.numCols,3);
        assertEquals(1,A.nz_length);
        assertEquals(0.3,A.get(1,2),UtilEjml.TEST_F64);
    }

    @Test
    public void removeZeros_one_random() {
        for (int i = 0; i < 40; i++) {
            int rows = rand.nextInt(10)+1;
            int cols = rand.nextInt(10)+1;

            int nz = RandomMatrices_DSCC.nonzero(rows,cols,0.02,0.5,rand);
            DMatrixSparseCSC A = RandomMatrices_DSCC.rectangle(rows,cols,nz,-1,1,rand);

            ImplCommonOps_DSCC.removeZeros(A,0.2);
            assertTrue(CommonOps_DSCC.checkStructure(A));
            assertEquals(A.numRows,rows);
            assertEquals(A.numCols,cols);
            for (int j = 0; j < rows; j++) {
                for (int k = 0; k < cols; k++) {
                    double val = A.get(j,k);
                    assertTrue("val = "+val,Math.abs(val) > 0.2 || val == 0);
                }
            }
        }
    }

    @Test
    public void symmLowerToFull() {
        IGrowArray gw = new IGrowArray();
        int sizes[] = {1,2,5,10};

        for (int i = 0; i < 20; i++) {
            for( int N : sizes ) {
                int nz = RandomMatrices_DSCC.nonzero(N,N/2,0.1,1.0,rand);
                DMatrixSparseCSC A = RandomMatrices_DSCC.triangleLower(N,0,nz,-1,1,rand);
                DMatrixSparseCSC B = new DMatrixSparseCSC(0,0);

                ImplCommonOps_DSCC.symmLowerToFull(A,B,gw);
                assertTrue(CommonOps_DSCC.checkStructure(B));

                for (int row = 0; row < N; row++) {
                    for (int col = 0; col <= row; col++) {
//                        System.out.println(row+" "+col);
                        assertEquals(A.get(row,col), B.get(row,col), UtilEjml.TEST_F64);
                        assertEquals(A.get(row,col), B.get(col,row), UtilEjml.TEST_F64);
                    }
                }
            }
        }
    }
}