Browsing through code that uses ojAlgo it’s quite common to see it being used inefficiently or at least not the intended way. Here’s an example code line, seen in the wild, that demonstrates common problems.
Primitive64Matrix selectedColumns = Primitive64Matrix.FACTORY.rows(initialMatrix.logical().limits(-1, 5).get().toRawCopy2D());

That single line, combined with how the selectedColumns matrix was later used, demonstrates several common mistakes. Let’s try to explain using a code example.

Code Example

Summary

  1. Stop thinking in terms of, converting to/from, double[][] and just use the ojAlgo data structures directly
  2. ojAlgo has special features that enable writing exceptionally memory efficient code. These feature are not well known/understood. To get the most out of ojAlgo you should become familiar with them.

Example – Memory Efficiency

Console Output

class ElementConsumersAndSuppliers
ojAlgo
2021-08-24


Iteration 1
The matrix [E] = [A][B] will have 5 rows and 9 columns.
The matrix [H] will have 9 rows and 5 columns.
Resulting D
  1.342196 -0.080722  0.942705  1.326401    0.0343
  0.952738  0.004302  0.680059  1.420024  0.749506
  -0.42315 -0.099356  0.602592 -0.128131  0.197995
 -0.874579 -1.989023 -1.479145 -1.963914 -1.480745
  0.403442  0.120605   0.38933  0.028234  0.202146
 -0.377875 -0.346078  0.662662  0.603232 -0.249229
   0.71807 -0.053794  0.956435  1.288518  0.184963
 -0.347932 -0.410419 -0.410582 -0.175535 -0.176471
 -1.163575 -0.363667 -0.779835  -0.91265 -0.809488

Is it full rank? true

Iteration 2
The matrix [E] = [A][B] will have 5 rows and 9 columns.
The matrix [H] will have 9 rows and 5 columns.
Resulting D
 -0.875651 -1.121959 -1.061803  -0.37018 -0.581922
 -0.021286  0.438606  0.439868 -0.348148 -0.243602
  0.115525  0.604465  0.305096 -0.068395 -0.584497
  0.755863  1.222269   0.56302  0.486334  0.292702
  0.203498  0.421592  0.160509  0.535417  0.587083
  0.779344  0.502896  0.338471  0.741886   0.28547
  1.077329  0.066448  0.018712  0.466584  0.771742
 -0.889879 -1.223156 -0.555143 -0.583784 -0.297454
  0.269486 -0.064799  0.055584  0.161476  0.126919

Is it full rank? true

Iteration 3
The matrix [E] = [A][B] will have 5 rows and 9 columns.
The matrix [H] will have 9 rows and 5 columns.
Resulting D
  0.201721 -0.647097  0.051457 -1.154906 -0.779558
 -0.623876 -1.614637 -0.886076 -1.052066 -1.868837
 -1.012378 -0.796751 -0.001973 -0.228281 -0.680911
 -0.211605  0.002338  0.524016  0.490409 -0.281095
  0.091177 -0.117529  0.522852  0.167912  0.201598
  0.263462  1.078214  0.049482  -0.31292  0.781295
 -0.484798  1.122343  0.285031  0.254435  1.012248
 -0.376376 -0.008645 -0.693459 -0.621088 -0.587739
  1.441024 -0.648415 -0.946848 -0.967546 -0.875984

Is it full rank? true

In addition it’s very much recommended to read the posts Linear Algebra Introduction.

Leave a Reply