FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[Original HTTPS Page]
GenericTensor/Sample/MatrixWrapper.cs at master · asc-community/GenericTensor · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
asc-community
/
GenericTensor
Public
Notifications
You must be signed in to change notification settings
Fork
5
Star
52
Code
Issues
11
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
GenericTensor
/
Sample
/
MatrixWrapper.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
108 lines (87 loc) · 3.5 KB
Breadcrumbs
GenericTensor
/
Sample
/
MatrixWrapper.cs
Copy path
File metadata and controls
108 lines (87 loc) · 3.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
(
)
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL