Advertisement
amaranthos

Matrix Lib

Feb 11th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.53 KB | None | 0 0
  1. module Matrix;
  2.  
  3. import std.stdio;
  4. import std.conv;
  5.  
  6. class Matrix(T, int height, int width) {
  7.     alias Matrix!(T, height, width) matType;
  8.     T[height * width] elements;
  9.  
  10.     int Height() const @property{
  11.         return height;
  12.     }
  13.  
  14.     int Width() const @property{
  15.         return width;
  16.     }
  17.  
  18.     ref T Element(uint x, uint y) {
  19.         if(x + y * width < elements.length)
  20.             return elements[x + y * width];
  21.  
  22.         throw new Exception ("FUCK, Index out of range!!!1!!11!");
  23.     }
  24.  
  25.     ref T opIndex(size_t x, size_t y){
  26.         return Element(x,y);
  27.     }
  28.  
  29.     matType opBinary(string s) (matType rhs) if(s =="-", s =="+") {
  30.         auto m = new matType;
  31.  
  32.         foreach(i; 0..elements.length) {
  33.             if(s=="+") {
  34.                 m.elements[i] = elements[i] + rhs.elements[i];
  35.             }
  36.             else if(s=="-") {
  37.                 m.elements[i] = elements[i] - rhs.elements[i];
  38.             }
  39.         }
  40.  
  41.         return m;
  42.     }
  43.  
  44.     Matrix!(T, height, rhsWidth) opBinary(string s, RHST, int rhsHeight, int rhsWidth) (Matrix!(RHST, rhsHeight, rhsWidth) rhs)
  45.         if(width == rhsHeight && s == "*") {
  46.         auto result = new Matrix!(T, height, rhsWidth);
  47.  
  48.         for(size_t i = 0; i < height; i++) {
  49.            
  50.             for (size_t j = 0; j < rhs.Width; j++) {
  51.                
  52.                 T sum = 0;
  53.                
  54.                 for(int k = 0; k < width; k++) {
  55.                     sum += Element(i, k) * rhs.Element(k, j);
  56.                    
  57.                     writeln(Element(i, k), " * " ,rhs.Element(k, j), " + ");
  58.  
  59.                 }
  60.                 result[i,j] = sum;
  61.             }
  62.         }
  63.         return result;
  64.     }
  65.  
  66.     matType opOpAssign(string s) (matType rhs) {
  67.         foreach(i; 0..elements.length) {
  68.             if(s=="+") {
  69.                 elements[i] += rhs.elements[i];
  70.             }
  71.             else if(s=="-") {
  72.                 elements[i] -= rhs.elements[i];
  73.             }
  74.         }
  75.         return this;
  76.     }
  77.  
  78.     matType opUnary(string s)() if(s=="-") {
  79.         auto m = new matType;
  80.         foreach(i; 0..elements.length) {
  81.             m.elements[i] = -elements[i];
  82.         }
  83.         return m;
  84.     }
  85.  
  86.  
  87.  
  88.     override string toString() const{
  89.         string s;
  90.         s ~= "[";
  91.  
  92.         foreach(i; 0..elements.length) {
  93.             s ~= to!string(elements[i]);
  94.            
  95.             if(i != elements.length-1)
  96.                 s ~= ", ";
  97.  
  98.             if(i % width == width - 1 && i != elements.length-1)
  99.                 s ~= "\n";
  100.         }
  101.         s ~= "]\n";
  102.         return s;
  103.     }
  104. }
  105.  
  106. alias Matrix!(double, 3,3) Matrix3;
  107.  
  108. void main() {
  109.     try{
  110.         auto m = new Matrix!(int, 4, 4);
  111.  
  112.         for(int i = 0; i < m.Width; i++) {
  113.             for (int j = 0; j < m.Height; j++) {
  114.                 m[i,j] = i;
  115.             }
  116.         }
  117.  
  118.         auto n = new Matrix!(int, 4, 4);
  119.  
  120.         for(int i = 0; i < n.Width; i++) {
  121.             for (int j = 0; j < n.Height; j++) {
  122.                
  123.                 n[i,j] = -j;
  124.             }
  125.         }
  126.  
  127.         writeln(n);
  128.         writeln(m);
  129.  
  130.         auto o = n * m;
  131.  
  132.         writeln(o);
  133.  
  134.  
  135.     }catch(Exception e){
  136.         writeln(e.msg);
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement