-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMatrixWrapper.cs
More file actions
108 lines (87 loc) · 3.5 KB
/
Copy pathMatrixWrapper.cs
File metadata and controls
108 lines (87 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
*
* This file shows how the user of this library is supposed to write their own
* wrappers for GenTensors.
*
*/
using GenericTensor.Core;
namespace Sample
{
/// <summary>
/// This class is only supposed to be used for matrix multiplication
/// </summary>
public sealed class MyMatrix
{
// if you want to use a built-in type with the same operations as they provide,
// you can use default wrappers like IntWrapper, FloatWrapper, DoubleWrapper,
// and other in DefaultWrappers.cs
private readonly GenTensor<double, MyCustomDoubleTypeWrapper> inner;
public MyMatrix(int width, int height)
// Many constructors and functions may be directly redirected in one
// defined in GenericTensor, but you are free to write your own new method
: this(GenTensor<double, MyCustomDoubleTypeWrapper>.CreateMatrix(width, height))
{
}
private MyMatrix(GenTensor<double, MyCustomDoubleTypeWrapper> newInner)
=> inner = newInner;
public double this[int x, int y]
{
// You may add bound check here, but they all are checked for you in inner tensor
get => inner[x, y];
set => inner[x, y] = value;
}
// MatrixMultiply only requires Add, Multiply, and CreateZero defined for your primitives
public static MyMatrix operator *(MyMatrix a, MyMatrix b)
=> new MyMatrix(GenTensor<double, MyCustomDoubleTypeWrapper>.MatrixMultiply(a.inner, b.inner));
// this will require ToString to be defined in your primitive wrapper
public override string ToString()
=> inner.ToString();
// Make sure that all required operations are implemented
// as there is no compile-time check on whether a method is implemented
// and it will throw NotImplementedException if not
private struct MyCustomDoubleTypeWrapper : IOperations<double>
{
public double Add(double a, double b) => a + b;
public double Multiply(double a, double b) => a * b;
public double CreateZero() => 0;
// It's better to somehow define it as it will make your debug-time easier :)
public string ToString(double a) => a.ToString();
public double Subtract(double a, double b)
{
throw new System.NotImplementedException();
}
public double Negate(double a)
{
throw new System.NotImplementedException();
}
public double Divide(double a, double b)
{
throw new System.NotImplementedException();
}
public double CreateOne()
{
throw new System.NotImplementedException();
}
public double Copy(double a)
{
throw new System.NotImplementedException();
}
public bool AreEqual(double a, double b)
{
throw new System.NotImplementedException();
}
public bool IsZero(double a)
{
throw new System.NotImplementedException();
}
public byte[] Serialize(double a)
{
throw new System.NotImplementedException();
}
public double Deserialize(byte[] data)
{
throw new System.NotImplementedException();
}
}
}
}