package org.kevoree.modeling.util.maths.newMatrix;

import org.junit.Assert;
import org.junit.Test;
import org.kevoree.modeling.util.maths.matrix.CommonOps;
import org.kevoree.modeling.util.maths.matrix.DenseMatrix64F;
import org.kevoree.modeling.util.maths.matrix.TransposeAlgs;
import org.kevoree.modeling.util.maths.structure.KArray2D;
import org.kevoree.modeling.util.maths.structure.impl.NativeArray2D;

import java.util.Random;


/**
 * Created by assaad on 27/08/15.
 */
/** @ignore ts */
public class SimpleMatrixText {
    @Test
    public void transposeTest() {
        NativeArray2D matA= new NativeArray2D(3,5);

        //test normal transpose
        int k=1;
        for(int i=0;i<matA.nbRows();i++){
            for(int j=0;j<matA.nbColumns();j++){
                matA.set(i,j,k);
                k++;
            }
        }

        KArray2D matB= SimpleMatrix.transpose(matA);

        Assert.assertTrue(matA.nbColumns()==matB.nbRows());
        Assert.assertTrue(matA.nbRows()==matB.nbColumns());

        for(int i=0;i<matA.nbRows();i++) {
            for (int j = 0; j < matA.nbColumns(); j++) {
                Assert.assertTrue(matA.get(i,j)==matB.get(j,i));
            }
        }

        //test square transpose

        DenseMatrix64F test = new DenseMatrix64F(5,5);


        matA= new NativeArray2D(5,5);

        k=1;
        for(int i=0;i<matA.nbRows();i++){
            for(int j=0;j<matA.nbColumns();j++){
                matA.set(i,j,k);
                test.set(i,j,k);
                k++;
            }
        }

        TransposeAlgs.square(test);

        matB= SimpleMatrix.transpose(matA);
        Assert.assertTrue(matA.nbColumns()==matB.nbRows());
        Assert.assertTrue(matA.nbRows()==matB.nbColumns());

        for(int i=0;i<matA.nbRows();i++) {
            for (int j = 0; j < matA.nbColumns(); j++) {
                Assert.assertTrue(matA.get(i,j)==matB.get(j,i));
            }
        }

        //test BIG transpose
        matA= new NativeArray2D(380,381);

        k=1;
        for(int i=0;i<matA.nbRows();i++){
            for(int j=0;j<matA.nbColumns();j++){
                matA.set(i,j,k);
                k++;
            }
        }

        matB= SimpleMatrix.transpose(matA);
        Assert.assertTrue(matA.nbColumns()==matB.nbRows());
        Assert.assertTrue(matA.nbRows()==matB.nbColumns());

        for(int i=0;i<matA.nbRows();i++) {
            for (int j = 0; j < matA.nbColumns(); j++) {
                Assert.assertTrue(matA.get(i,j)==matB.get(j,i));
            }
        }
    }


    private void traditional(KArray2D matA, KArray2D matB, KArray2D matC){
        for (int i=0;i<matC.nbRows();i++){
            for(int j=0;j<matC.nbColumns();j++){
                for(int k=0;k<matA.nbColumns();k++){
                    matC.add(i,j,matA.get(i,k)*matB.get(k,j));
                }
            }
        }
    }

    @Test
    public void multiplyTest() {

        int[] dimA={300,1000};
        int[] dimB={1000,500};

        NativeArray2D matA= new NativeArray2D(dimA[0],dimA[1]);

        Random rand=new Random();
        for(int i=0;i<matA.nbRows();i++){
            for(int j=0;j<matA.nbColumns();j++){
                matA.set(i,j,rand.nextDouble());
            }
        }

        NativeArray2D matB= new NativeArray2D(dimB[0],dimB[1]);

        for(int i=0;i<matB.nbRows();i++){
            for(int j=0;j<matB.nbColumns();j++){
                matB.set(i,j,rand.nextDouble());
            }
        }

        NativeArray2D matC= new NativeArray2D(matA.nbRows(),matB.nbColumns());

        long start=System.nanoTime();
        traditional(matA, matB,matC);
        long end=System.nanoTime();
        System.out.println("Java " + ((double) (end - start)) / 1000000 + " ms");



        start=System.nanoTime();
        KArray2D matRes = SimpleMatrix.multiply(matA, matB);
        end=System.nanoTime();
        System.out.println("EJML ported "+((double)(end-start))/1000000+ " ms");


        Assert.assertTrue(matRes.nbRows()==matC.nbRows());
        Assert.assertTrue(matRes.nbColumns()==matC.nbColumns());

        for(int i=0;i<3;i++) {
            for (int j = 0; j < 2; j++) {
                Assert.assertTrue(matRes.get(i,j)==matC.get(i,j));
            }
        }



    }
}

@@ -2958,14 +2958,12 @@
 =%7B30
-0
 ,100
-0
 %7D;%0A 
@@ -2988,13 +2988,11 @@
 %7B100
-0
 ,50
-0
 %7D;%0A%0A
@@ -3569,45 +3569,8 @@
 );%0A%0A
-        long start=System.nanoTime();
 %0A   
@@ -3616,327 +3616,69 @@
     
-long end=System.nanoTime();%0A        System.out.println(%22Java %22 + ((double) (end - start)) / 1000000 + %22 ms%22);%0A%0A%0A%0A        start=System.nanoTime();%0A        KArray2D matRes = SimpleMatrix.multiply(matA, matB);%0A        end=System.nanoTime();%0A        System.out.println(%22EJML ported %22+((double)(end-start))/1000000+ %22 ms%22);%0A
+KArray2D matRes = SimpleMatrix.multiply(matA, matB);%0A        
 %0A%0A  

update test