File: TestDMatrixSparseCSC.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 (157 lines) | stat: -rw-r--r-- 4,738 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
/*
 * 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.data;

import org.ejml.ops.ConvertDMatrixStruct;
import org.ejml.sparse.csc.CommonOps_DSCC;
import org.ejml.sparse.csc.RandomMatrices_DSCC;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 * @author Peter Abeles
 */
public class TestDMatrixSparseCSC extends GenericTestsDMatrixSparse {

    @Override
    public DMatrixSparse createSparse(int numRows, int numCols) {
        return new DMatrixSparseCSC(numRows,numCols,10);
    }

    @Override
    public DMatrixSparse createSparse(DMatrixSparseTriplet orig) {
        return ConvertDMatrixStruct.convert(orig,(DMatrixSparseCSC)null);
    }

    @Override
    public boolean isStructureValid(DMatrixSparse m) {
        return CommonOps_DSCC.checkStructure((DMatrixSparseCSC)m);
    }

    @Test
    public void constructor_veryLarge() {
        DMatrixSparseCSC a = new DMatrixSparseCSC(1_000_000_000,1_000_000_000,4);

        assertEquals(0,a.nz_length);
        assertEquals(1_000_000_000,a.numCols);
        assertEquals(1_000_000_000,a.numRows);
        assertEquals(4,a.nz_values.length);
        assertEquals(4,a.nz_rows.length);
    }

    @Test
    public void growMaxLength_veryLarge() {
        DMatrixSparseCSC a = new DMatrixSparseCSC(1_000_000_000,1_000_000_000,4);

        a.growMaxLength(10,false);
        assertEquals(0,a.nz_length);
        assertEquals(1_000_000_000,a.numCols);
        assertEquals(1_000_000_000,a.numRows);
        assertEquals(10,a.nz_values.length);
        assertEquals(10,a.nz_rows.length);
    }

    @Test
    public void reshape_row_col_length() {
        DMatrixSparseCSC a = new DMatrixSparseCSC(2,3,4);

        a.reshape(1,2,3);
        assertTrue(CommonOps_DSCC.checkStructure(a));
        assertEquals(1,a.numRows);
        assertEquals(2,a.numCols);
        assertEquals(4,a.nz_values.length);
        assertEquals(0,a.nz_length);

        a.reshape(4,1,10);
        assertTrue(CommonOps_DSCC.checkStructure(a));
        assertEquals(4,a.numRows);
        assertEquals(1,a.numCols);
        assertEquals(4,a.nz_values.length);
        assertEquals(0,a.nz_length);
    }

    @Test
    public void sortIndices() {
        DMatrixSparseCSC a = RandomMatrices_DSCC.rectangle(5,4,20,-1,1,rand);

        // make sure it's not sorted correctly
        a.nz_rows[0]=2;
        a.nz_rows[2]=0;
        assertFalse(CommonOps_DSCC.checkIndicesSorted(a));
        a.indicesSorted = false;

        // now sort it and see if its fixed
        a.sortIndices(null);

        assertTrue(CommonOps_DSCC.checkIndicesSorted(a));
        assertTrue(a.indicesSorted);
    }

    @Test
    public void growMaxColumns() {
        DMatrixSparseCSC a = RandomMatrices_DSCC.rectangle(5,4,20,-1,1,rand);
        a.col_idx[0] = 5;
        a.col_idx[1] = 15;

        // grow when resize isn't needed
        a.growMaxColumns(3,false);
        assertEquals(5,a.col_idx[0]);
        assertEquals(15,a.col_idx[1]);

        // shouldn't declare a new array
        a.growMaxColumns(4,false);
        assertEquals(5,a.col_idx[0]);
        assertEquals(15,a.col_idx[1]);

        // resize is needed now
        a.growMaxColumns(5,true);
        assertEquals(5,a.col_idx[0]);
        assertEquals(15,a.col_idx[1]);

        a.growMaxColumns(6,false);
        assertEquals(0,a.col_idx[0]);
        assertEquals(0,a.col_idx[1]);
    }

    /**
     * The matrix is already sorted.  See if it is still sorted after set has been called.
     */
    @Test
    public void set_sorted() {
        DMatrixSparseCSC a = new DMatrixSparseCSC(4,5,0);
        a.indicesSorted = true;

        a.set(1,2, 1);
        assertTrue(a.indicesSorted);
        assertTrue(CommonOps_DSCC.checkStructure(a));

        a.set(0,2, 1);
        assertTrue(a.indicesSorted);
        assertTrue(CommonOps_DSCC.checkStructure(a));

        a.set(3,2, 1);
        assertTrue(a.indicesSorted);
        assertTrue(CommonOps_DSCC.checkStructure(a));

        a.set(2,2, 1);
        assertTrue(a.indicesSorted);
        assertTrue(CommonOps_DSCC.checkStructure(a));
    }
}