Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sounds like you need a sparse matrix. Others have already suggested good 3rd party implementations that may suite your needs...</p> <p>Depending on your applications, you could get away without a third-party matrix library by just using a Map as a backing-store for your matrix data. Kind of...</p> <pre><code>public class SparseMatrix&lt;T&gt; { private T defaultValue; private int m; private int n; private Map&lt;Integer, T&gt; data = new TreeMap&lt;Integer, T&gt;(); /// create a new matrix with m rows and n columns public SparseMatrix(int m, int n, T defaultValue) { this.m = m; this.n = n; this.defaultValue = defaultValue; } /// set value at [i,j] (row, col) public void setValueAt(int i, int j, T value) { if (i &gt;= m || j &gt;= n || i &lt; 0 || j &lt; 0) throw new IllegalArgumentException( "index (" + i + ", " +j +") out of bounds"); data.put(i * n + j, value); } /// retrieve value at [i,j] (row, col) public T getValueAt(int i, int j) { if (i &gt;= m || j &gt;= n || i &lt; 0 || j &lt; 0) throw new IllegalArgumentException( "index (" + i + ", " +j +") out of bounds"); T value = data.get(i * n + j); return value != null ? value : defaultValue; } } </code></pre> <p>A simple test-case illustrating the SparseMatrix' use would be:</p> <pre><code>public class SparseMatrixTest extends TestCase { public void testMatrix() { SparseMatrix&lt;Float&gt; matrix = new SparseMatrix&lt;Float&gt;(100000, 100000, 0.0F); matrix.setValueAt(1000, 1001, 42.0F); assertTrue(matrix.getValueAt(1000,1001) == 42.0); assertTrue(matrix.getValueAt(1001,1000) == 0.0); } } </code></pre> <p>This is not the most efficient way of doing it because every non-default entry in the matrix is stored as an Object. Depending on the number of actual values you are expecting, the simplicity of this approach might trump integrating a 3rd-party solution (and possibly dealing with its License - again, depending on your situation).</p> <p>Adding matrix-operations like multiplication to the above SparseMatrix implementation should be straight-forward (and is left as an exercise for the reader ;-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload