11from .mojmelo_matmul import matmul
2- from memory import memcpy, memset_zero
3- import random
2+ from std. memory import memcpy, memset_zero
3+ import std.random as random
44
55struct Matrix (Copyable , ImplicitlyCopyable , Sized ):
66 var height : Int
@@ -11,7 +11,7 @@ struct Matrix(Copyable, ImplicitlyCopyable, Sized):
1111
1212 # initialize from UnsafePointer
1313 @always_inline
14- fn __init__ [src : DType = DType.float32](out self , data : UnsafePointer[Scalar[src], MutAnyOrigin], height : Int, width : Int, order : String = ' c' ):
14+ def __init__ [src : DType = DType.float32](out self , data : UnsafePointer[Scalar[src], MutAnyOrigin], height : Int, width : Int, order : String = ' c' ):
1515 self .height = height
1616 self .width = width
1717 self .size = height * width
@@ -24,7 +24,7 @@ struct Matrix(Copyable, ImplicitlyCopyable, Sized):
2424
2525 # initialize by copying from UnsafePointer
2626 @always_inline
27- fn __init__ (out self , height : Int, width : Int, data : UnsafePointer[Float32, MutAnyOrigin] = UnsafePointer[Float32, MutAnyOrigin](), order : String = ' c' ):
27+ def __init__ (out self , height : Int, width : Int, data : UnsafePointer[Float32, MutAnyOrigin] = UnsafePointer[Float32, MutAnyOrigin](), order : String = ' c' ):
2828 self .height = height
2929 self .width = width
3030 self .size = height * width
@@ -33,27 +33,27 @@ struct Matrix(Copyable, ImplicitlyCopyable, Sized):
3333 if data:
3434 memcpy(dest = self .data, src = data, count = self .size)
3535
36- fn __copyinit__ (out self , other : Self):
37- self .height = other .height
38- self .width = other .width
39- self .size = other .size
36+ def __init__ (out self , * , copy : Self):
37+ self .height = copy .height
38+ self .width = copy .width
39+ self .size = copy .size
4040 self .data = alloc[Float32](self .size)
41- self .order = other .order
42- memcpy(dest = self .data, src = other .data, count = self .size)
41+ self .order = copy .order
42+ memcpy(dest = self .data, src = copy .data, count = self .size)
4343
44- fn __moveinit__ (out self , deinit existing : Self):
45- self .height = existing .height
46- self .width = existing .width
47- self .size = existing .size
48- self .data = existing .data
49- self .order = existing .order
50- # existing .height = existing .width = existing .size = 0
51- # existing .order = ''
52- # existing .data = UnsafePointer[Float32, MutAnyOrigin]()
44+ def __init__ (out self , * , deinit take : Self):
45+ self .height = take .height
46+ self .width = take .width
47+ self .size = take .size
48+ self .data = take .data
49+ self .order = take .order
50+ # take .height = take .width = take .size = 0
51+ # take .order = ''
52+ # take .data = UnsafePointer[Float32, MutAnyOrigin]()
5353
5454 # access an element
5555 @always_inline
56- fn __getitem__ (self , row : Int, column : Int) raises -> Float32:
56+ def __getitem__ (self , row : Int, column : Int) raises -> Float32:
5757 var loc : Int
5858 if self .order == ' c' :
5959 loc = (row * self .width) + column
@@ -64,16 +64,16 @@ struct Matrix(Copyable, ImplicitlyCopyable, Sized):
6464 return self .data[loc]
6565
6666 @always_inline
67- fn __del__ (deinit self ):
67+ def __del__ (deinit self ):
6868 if self .data:
6969 self .data.free()
7070
7171 @always_inline
72- fn __len__ (self ) -> Int:
72+ def __len__ (self ) -> Int:
7373 return self .size
7474
7575 @always_inline
76- fn __mul__ (self , rhs : Self) raises -> Self:
76+ def __mul__ (self , rhs : Self) raises -> Self:
7777 if self .width != rhs.height:
7878 raise Error(' Error: Cannot multiply matrices with shapes (' + String(self .height) + ' , ' + String(self .width) + ' ) and (' + String(rhs.height) + ' , ' + String(rhs.width) + ' )' )
7979
@@ -102,18 +102,18 @@ struct Matrix(Copyable, ImplicitlyCopyable, Sized):
102102 return Matrix(C.data, self .height, rhs.width)
103103
104104 @always_inline
105- fn __imul__ (mut self , rhs : Self) raises :
105+ def __imul__ (mut self , rhs : Self) raises :
106106 self = self * rhs
107107
108108 @ staticmethod
109109 @always_inline
110- fn zeros (height : Int, width : Int, order : String = ' c' ) -> Matrix:
110+ def zeros (height : Int, width : Int, order : String = ' c' ) -> Matrix:
111111 var mat = Matrix(height, width, order = order)
112112 memset_zero(mat.data, mat.size)
113113 return mat^
114114
115115 @ staticmethod
116- fn random (height : Int, width : Int, order : String = ' c' ) -> Matrix:
116+ def random (height : Int, width : Int, order : String = ' c' ) -> Matrix:
117117 random.seed()
118118 var mat = Matrix(height, width, order = order)
119119 random.rand(mat.data, mat.size, min = 0.0 , max = 1.0 )
0 commit comments