forked from AndrewLuckett/MatrixPyDll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.py
More file actions
134 lines (90 loc) · 3.38 KB
/
Matrix.py
File metadata and controls
134 lines (90 loc) · 3.38 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
Author: Andrew Luckett
License: CC-BY
Module name: Matrix
This module loads the Matrix.dll library and binds the functions to produce a matrix class.
The matrix class is fully immutable to produce a thread safe and logical implementation
This module requires that Matrix.dll be located in the same file path as it.
"""
import ctypes
import pathlib
#Load lib
path = str(pathlib.Path().absolute())
clib = ctypes.CDLL(path + "/Matrix.dll")
#TYPES
c_int = ctypes.c_int
c_point = ctypes.c_void_p
c_float = ctypes.c_double
c_bool = ctypes.c_bool
class Size(ctypes.Structure):
_fields_ = [("height", c_int), ("width", c_int)]
def __repr__(this):
return "Size(" + str(this.height) + ", " + str(this.width) + ")"
#Params
res = [c_point, c_int, Size, c_float, c_bool]
args = [[Size, c_float, c_int], [c_point, c_point], [c_point],
[c_point, c_float], [c_point, c_int, c_int], [Size, c_point]]
#Pre formed func defs
#Format name : (res, args)
funcs = {"newMat":(0,0), "mult":(0,1), "add":(0,1), "sub":(0,1), "equ":(4,1), "del":(4,2),
"sca":(0,3), "getSize":(2,2), "getValue":(3,4), "generate":(0,5)}
#Define funcs
for k,v in funcs.items():
fres = res[v[0]]
fargs = args[v[1]]
p = ctypes.WINFUNCTYPE(fres, *fargs)
funcs[k] = p((k, clib))
#Cleanup
del path, clib, c_int, c_point, c_bool
del res, args, k, v, fres, fargs, p
toPointer = ctypes.pointer
del pathlib, ctypes
#Keeping c_float and ctypes.pointer for toFloatP func
#Util
def toFloatP(length, inp):
fa = (c_float * length)(*inp)
return toPointer(fa)
class Matrix:
def __init__(this, size, dat = None):
if dat == None: #Must be just a pointer
if not size:
raise Exception("Null pointer: Produced by invalid process")
this.pointer = size
elif type(dat) == list and type(size) == tuple:
length = size[0] * size[1]
this.pointer = funcs["generate"](Size(size[0], size[1]), toFloatP(length, dat))
elif type(size) == tuple:
this.pointer = funcs["newMat"](Size(size[0], size[1]), dat, False)
else:
raise Exception("eek")
def getValue(this, *pos):
return funcs["getValue"](this.pointer, pos[0], pos[1])
def getSize(this):
siz = funcs["getSize"](this.pointer)
return siz.height, siz.width
def __eq__(this, that):
if type(that) != Matrix:
return False
return funcs["equ"](this.pointer, that.pointer)
def __add__(this, that):
if type(that) != Matrix:
raise Exception("Invalid type")
return Matrix(funcs["add"](this.pointer, that.pointer))
def __sub__(this, that):
if type(that) != Matrix:
raise Exception("Invalid type")
return Matrix(funcs["sub"](this.pointer, that.pointer))
def __mul__(this, that):
if type(that) == Matrix:
return Matrix(funcs["mult"](this.pointer, that.pointer))
elif not type(that) in [int, float, complex]:
raise Exception("Invalid type")
return Matrix(funcs["sca"](this.pointer, that))
def __neg__(this):
return Matrix(funcs["sca"](this.pointer, -1))
def __pos__(this):
return this #Dont gen new or python will prematurely delete data
def __del__(this):
funcs["del"](this.pointer)
def __repr__(this):
return "Matrix" + str(this.getSize())