Advertisement
Guest User

Untitled

a guest
Sep 25th, 2021
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const range = (count) => Array.from(Array(count).keys());
  2.  
  3. class Vector {
  4.  
  5.     static Add(A, B) {
  6.         const hA = A.length;
  7.         const hB = B.length;
  8.  
  9.         if (hA != hB)
  10.         {
  11.             throw "A height != B height";
  12.         }
  13.  
  14.         return range(hA).map((_, i) => A[i] + B[i]);
  15.     }
  16.  
  17.     static Sub(A, B) {
  18.         const hA = A.length;
  19.         const hB = B.length;
  20.  
  21.         if (hA != hB)
  22.         {
  23.             throw "A height != B height";
  24.         }
  25.  
  26.         return range(hA).map((_, i) => A[i] - B[i]);
  27.     }
  28.  
  29.     static Translate(A, shift) {
  30.         const wA = A.length;
  31.  
  32.         const R = range(wA).map((_, i) => A[i] + shift);
  33.         return R;                        
  34.     }    
  35.  
  36.     static Scale(A, scale) {
  37.         const wA = A.length;
  38.  
  39.         const R = range(wA).map((_, i) => A[i] * scale);
  40.         return R;                        
  41.     }            
  42.  
  43.     static FromMatrixRow(A, k = 0)
  44.     {
  45.         const wA = A.length;
  46.  
  47.         const C = range(wA).map((_, i) =>A[i][k]);
  48.         return C;
  49.     }  
  50.  
  51.     static FromMatrixColumn(A, k = 0)
  52.     {
  53.         if (!A)
  54.         {
  55.             throw "No matrix";
  56.         }
  57.  
  58.         if (k >= A.length)
  59.         {
  60.             throw "Out of index";
  61.         }
  62.  
  63.         if (!A[k])
  64.         {
  65.             throw "No Data at index: " + k;
  66.         }        
  67.  
  68.         const wA = A[k].length;
  69.  
  70.         const C = range(wA).map((_, i) =>A[k][i]);
  71.         return C;
  72.     }                
  73.    
  74.     static IndexOfMaxValue(A)
  75.     {
  76.         const wA = A.length;
  77.        
  78.         var index = 0;
  79.         var v = A[0];
  80.         for (let i = 1; i < wA; i++)
  81.         {
  82.             if (A[i] > v)
  83.             {
  84.                 v = A[i];
  85.                 index = i;
  86.             }
  87.         }
  88.  
  89.         return index;
  90.     }    
  91.  
  92.     static MaxValueOf(A)
  93.     {
  94.         const wA = A.length;
  95.        
  96.         var v = A[0];
  97.         for (let i = 1; i < wA; i++)
  98.         {
  99.             if (A[i] > v)
  100.             {
  101.                 v = A[i];
  102.             }
  103.         }
  104.  
  105.         return v;
  106.     }      
  107. }      
  108.  
  109. class Matrix {
  110.  
  111.     static Dot(A, B) {
  112.         // Dot production
  113.         const wA = A[0].length;
  114.         const hA = A.length;
  115.         const wB = B[0].length;
  116.         const hB = B.length;
  117.  
  118.         if (wA != hB)
  119.         {
  120.             throw "A width != B height";
  121.         }
  122.  
  123.         const C = range(hA).map((_, i) => range(wB).map((_, j) => 0));
  124.  
  125.         for (let i = 0; i < hA; ++i)
  126.             for (let j = 0; j < wB; ++j) {
  127.                 let sum = 0;
  128.  
  129.                 for (let k = 0; k < wA; ++k) {
  130.                     const a = A[i][k];
  131.                     const b = B[k][j];
  132.                     sum += a * b;
  133.                 }
  134.  
  135.                 C[i][j] = sum;
  136.             }
  137.  
  138.         return C;                
  139.     }
  140.  
  141.     static Mul(A, B) {
  142.         // Dot production
  143.         const wA = A[0].length;
  144.         const hA = A.length;
  145.         const wB = B[0].length;
  146.         const hB = B.length;
  147.  
  148.         if (wA != wB || hA != hB)
  149.         {
  150.             throw "A width != B width, A height != B height";
  151.         }
  152.  
  153.         const C = range(hA).map((_, i) => range(wA).map((_, j) => A[i][j] * B[i][j]));
  154.         return C;
  155.     }            
  156.  
  157.     static Mul_ValueByVector(A, B) {
  158.         // Dot production
  159.         const wA = A[0].length;
  160.         const hA = A.length;
  161.         const wB = B[0].length;
  162.         const hB = B.length;
  163.  
  164.         if (hA != hB)
  165.         {
  166.             throw "A height != B height";
  167.         }
  168.  
  169.         const C = range(hA).map((_, i) => range(wA).map((_, j) => Vector.Scale(B[i], A[i][j])));
  170.         return C;
  171.     }  
  172.  
  173.     static Add(A, B) {
  174.         const wA = A[0].length;
  175.         const hA = A.length;
  176.         const wB = B[0].length;
  177.         const hB = B.length;
  178.  
  179.         if (wA != wB || hA != hB)
  180.         {
  181.             throw "A width != B width, A height != B height";
  182.         }
  183.  
  184.         const C = range(hA).map((_, i) => range(wA).map((_, j) => A[i][j] + B[i][j]));
  185.         return C;
  186.     }
  187.  
  188.     static Sub(A, B) {
  189.         const wA = A[0].length;
  190.         const hA = A.length;
  191.         const wB = B[0].length;
  192.         const hB = B.length;
  193.  
  194.         if (wA != wB || hA != hB)
  195.         {
  196.             throw "A width != B width, A height != B height";
  197.         }
  198.  
  199.         const C = range(hA).map((_, i) => range(wA).map((_, j) => A[i][j] - B[i][j]));
  200.         return C;
  201.     }
  202.  
  203.     static Transpose(A) {
  204.         const wA = A[0].length;
  205.         const hA = A.length;
  206.  
  207.         const T = range(wA).map((_, j) => range(hA).map((_, i) => A[i][j]));
  208.         return T;
  209.     }            
  210.  
  211.     static Random(w, h) {
  212.         const m = range(h).map((_, i) => range(w).map((_, j) => Math.random()));
  213.         return m;
  214.     }
  215.  
  216.     static Scale(A, scale) {
  217.         const wA = A[0].length;
  218.         const hA = A.length;
  219.  
  220.         const R = range(hA).map((_, i) => range(wA).map((_, j) => A[i][j] * scale));
  221.         return R;                        
  222.     }            
  223.  
  224.     static Translate(A, shift) {
  225.         const wA = A[0].length;
  226.         const hA = A.length;
  227.  
  228.         const R = range(hA).map((_, i) => range(wA).map((_, j) => A[i][j] + shift));
  229.         return R;                        
  230.     }        
  231.  
  232.     static Average(A) {
  233.         // A is array of matrixes
  234.         const hA = A.length;
  235.  
  236.         let C = A[0];
  237.         for (let i = 1; i < hA; i++)
  238.         {
  239.             C = Matrix.Add(C, A[i]);
  240.         }
  241.  
  242.         C = Matrix.Scale(C, 1/hA);
  243.  
  244.         return C;
  245.     }              
  246.  
  247.     static Sigmoid(A) {
  248.         const wA = A[0].length;
  249.         const hA = A.length;
  250.  
  251.         const R = range(hA).map((_, i) => range(wA).map((_, j) => 1 / (1 + Math.exp(-A[i][j]))));
  252.         return R;                                        
  253.     }
  254.  
  255.     static SigmoidDerivative(A) {
  256.         const wA = A[0].length;
  257.         const hA = A.length;
  258.  
  259.         const R = range(hA).map((_, i) => range(wA).map((_, j) => {
  260.             const sigmoid = 1 / (1 + Math.exp(-A[i][j]));
  261.             return sigmoid * (1 - sigmoid);
  262.         }));
  263.         return R;                  
  264.     }      
  265. }
  266.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement