# Found a Matrix library

**URL:** https://discourse.processing.org/t/found-a-matrix-library/2910
**Category:** Gallery
**Created:** [August 24, 2018, 6:44am UTC](https://discourse.processing.org/t/found-a-matrix-library/2910 "2018-08-24T06:44:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![HackinHarry](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@HackinHarry](https://discourse.processing.org/u/HackinHarry)
#### Post date: [August 24, 2018, 6:44am UTC](https://discourse.processing.org/t/found-a-matrix-library/2910/1 "2018-08-24T06:44:06Z")

</div>

I stumbled onto a library for manipulating and analyzing matrices. I’m pretty sure there are several options for doing this in Processing, but this option here works so I thought I would share the example.

First, save a sketch and then add a “code” directory. Download the Jama(java matrix library) jar file from this link: [https://math.nist.gov/javanumerics/jama/](https://math.nist.gov/javanumerics/jama/)

Then put this code into the saved sketch and save the downloaded jar file into the code directory of the sketch. If you download the zip file version, you get the source code and the documentation for the library.:

```auto
import Jama.*;
//import controlP5.*;
//import Jama.Matrix;
import java.util.Date;

//class MagicSquareExample {

   / **Generate magic square test matrix.** /

   public Matrix magic (int n) {

      double[][] M = new double[n][n];

      // Odd order

      if ((n % 2) == 1) {
         int a = (n+1)/2;
         int b = (n+1);
         for (int j = 0; j < n; j++) {
            for (int i = 0; i < n; i++) {
               M[i][j] = n*((i+j+a) % n) + ((i+2*j+b) % n) + 1;
            }
         }

      // Doubly Even Order

      } else if ((n % 4) == 0) {
         for (int j = 0; j < n; j++) {
            for (int i = 0; i < n; i++) {
               if (((i+1)/2)%2 == ((j+1)/2)%2) {
                  M[i][j] = n*n-n*i-j;
               } else {
                  M[i][j] = n*i+j+1;
               }
            }
         }

      // Singly Even Order

      } else {
         int p = n/2;
         int k = (n-2)/4;
         Matrix A = magic(p);
         for (int j = 0; j < p; j++) {
            for (int i = 0; i < p; i++) {
               double aij = A.get(i,j);
               M[i][j] = aij;
               M[i][j+p] = aij + 2*p*p;
               M[i+p][j] = aij + 3*p*p;
               M[i+p][j+p] = aij + p*p;
            }
         }
         for (int i = 0; i < p; i++) {
            for (int j = 0; j < k; j++) {
               double t = M[i][j]; M[i][j] = M[i+p][j]; M[i+p][j] = t;
            }
            for (int j = n-k+1; j < n; j++) {
               double t = M[i][j]; M[i][j] = M[i+p][j]; M[i+p][j] = t;
            }
         }
         double t = M[k][0]; M[k][0] = M[k+p][0]; M[k+p][0] = t;
         t = M[k][k]; M[k][k] = M[k+p][k]; M[k+p][k] = t;
      }
      return new Matrix(M);
   }

public String fixedWidthDoubletoString (double x, int w, int d) {
      java.text.DecimalFormat fmt = new java.text.DecimalFormat();
      fmt.setMaximumFractionDigits(d);
      fmt.setMinimumFractionDigits(d);
      fmt.setGroupingUsed(false);
      String s = fmt.format(x);
      while (s.length() < w) {
         s = " " + s;
      }
      return s;
   }

   public String fixedWidthIntegertoString (int n, int w) {
      String s = Integer.toString(n);
      while (s.length() < w) {
         s = " " + s;
      }
      return s;
   }

//ControlP5 cp5;
//ControlP5 controlP5;
//Textarea myTextarea;
   void setup() {
// fullscreen();

     size(1275, 750, OPENGL);
     
       /* myTextarea = cp5.addTextarea("txt")
                  .setPosition(450,10)
                  .setSize(800,700)
                  .setFont(createFont("arial",14))
                  .setLineHeight(14)
                  .setColor(color(255))
                  .setColorBackground(color(255,0))
                  .setColorForeground(color(255,100)); */
   }
void draw() {
 // public void main (String argv[]) {
//noLoop();
   /* 
    | Tests LU, QR, SVD and symmetric Eig decompositions.
    |
    | n = order of magic square.
    | trace = diagonal sum, should be the magic sum, (n^3 + n)/2.
    | max_eig = maximum eigenvalue of (A + A')/2, should equal trace.
    | rank = linear algebraic rank,
    | should equal n if n is odd, be less than n if n is even.
    | cond = L_2 condition number, ratio of singular values.
    | lu_res = test of LU factorization, norm1(L*U-A(p,:))/(n*eps).
    | qr_res = test of QR factorization, norm1(Q*R-A)/(n*eps).
    */

      print("\n Test of Matrix Class, using magic squares.\n");
      print(" See MagicSquareExample.main() for an explanation.\n");
      print("\n n trace max_eig rank cond lu_res qr_res\n\n");
 
      Date start_time = new Date();
      double eps = Math.pow(2.0,-52.0);
      for ( int n = 3;n <= 32;n++ ) {
        
         print(fixedWidthIntegertoString(n,7));
       
         Matrix M = magic(n);

         int t = (int) M.trace();
         
         print(fixedWidthIntegertoString(t,10));
         
         EigenvalueDecomposition E =
            new EigenvalueDecomposition(M.plus(M.transpose()).times(0.5));
         double[] d = E.getRealEigenvalues();
         print(fixedWidthDoubletoString(d[n-1],14,3));

         int r = M.rank();
         print(fixedWidthIntegertoString(r,7));
         
         double c = M.cond();
         print(c < 1/eps ? fixedWidthDoubletoString(c,12,3) :
            " Inf");
         
         LUDecomposition LU = new LUDecomposition(M);
         Matrix L = LU.getL();
         Matrix U = LU.getU();
         int[] p = LU.getPivot();
         Matrix R = L.times(U).minus(M.getMatrix(p,0,n-1));
         double res = R.norm1()/(n*eps);
         print(fixedWidthDoubletoString(res,12,3));
         
         QRDecomposition QR = new QRDecomposition(M);
         Matrix Q = QR.getQ();
         R = QR.getR();
         R = Q.times(R).minus(M);
         res = R.norm1()/(n*eps);
         print(fixedWidthDoubletoString(res,12,3));
        
         print("\n");
      } 
  
      Date stop_time = new Date();
      double etime = (stop_time.getTime() - start_time.getTime())/1000.;
      print("\nElapsed Time = " + 
         fixedWidthDoubletoString(etime,12,3) + " seconds\n");
      print("Adios\n");
}

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 22, 2019, 2:11pm UTC](https://discourse.processing.org/t/found-a-matrix-library/2910/2 "2019-01-22T14:11:50Z")

</div>

> **[Mathematics, Statistics and Computational Science at NIST](https://math.nist.gov/)**
>
> Gateway to organizations and services related to applied mathematics,
> statistics, and computational science at the National Institute of
> Standards and Technology (NIST).

> Sorry, but due to fact that the federal government is currently shut down, the website [math.nist.gov](http://math.nist.gov) will be unavailable until further notice. [Learn More](https://www.commerce.gov/news/blog/2018/12/shutdown-due-lapse-congressional-appropriations)  
> Local time is: Tuesday, 22-Jan-2019 09:09:23 EST  
> If you believe you received this page in error or have other questions, please send us an email with your issue to: [mcsdweb@nist.gov](mailto:mcsdweb@nist.gov). Please copy/paste the contents of this page. We will investigate and respond after operations resume.

looks like the computer also did not get payed ??

---

<div class="post-metadata">

### Author: ![dan850](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@dan850](https://discourse.processing.org/u/dan850)
#### Post date: [January 23, 2019, 8:15am UTC](https://discourse.processing.org/t/found-a-matrix-library/2910/3 "2019-01-23T08:15:17Z")

</div>

@kll Your comment made my night:-) Is there a demonstration somewhere of this library while we are waiting on the government i guess, like a tutorial or video?

---

<div class="post-metadata">

### Author: ![HackinHarry](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@HackinHarry](https://discourse.processing.org/u/HackinHarry)
#### Post date: [March 4, 2019, 1:49am UTC](https://discourse.processing.org/t/found-a-matrix-library/2910/4 "2019-03-04T01:49:14Z")

</div>

This link takes me directly to JAMA: [https://math.nist.gov/javanumerics/jama/](https://math.nist.gov/javanumerics/jama/)
