Skip to content

Commit

Permalink
Update the code to accommodate v0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
forFudan committed Oct 31, 2024
1 parent f9fa39b commit a605942
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
5 changes: 5 additions & 0 deletions numojo/core/mat/__init__.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""
Implements Matrix type (2-dimensional array)
"""

from .mat import *
28 changes: 5 additions & 23 deletions numojo/core/mat.mojo → numojo/core/mat/mat.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ manipulation, and it can also be more consistent with numpy.
For example:
- For `__getitem__`, inputting two `Int` returns a scalar,
inputting one `Int` returns a vector, and inputting no `Int`
returns a Matrix.
- For row-major and column-major matrices, it is easier to get the
values by the indices, as strides are only two numbers.
inputting one `Int` or no `Int` returns a Matrix.
- We do not need auxillary types `NDArrayShape` and `NDArrayStrides`
as the shape and strides information is fixed in length `Tuple[Int,Int]`.
Expand All @@ -26,8 +23,8 @@ the behavior of `NDArray` type and the `Matrix` type.
"""

from numojo.prelude import *
from .ndarray import NDArray
from memory import memcmp
from numojo.core.ndarray import NDArray
from memory import memcmp, memcpy
from sys import simdwidthof

# ===----------------------------------------------------------------------===#
Expand Down Expand Up @@ -685,7 +682,7 @@ struct Matrix[dtype: DType = DType.float64](Stringable, Formattable):
var ndarray = NDArray[dtype](
shape=List[Int](self.shape[0], self.shape[1]), order="C"
)
memcpy(ndarray.data, self._buf, ndarray.size())
memcpy(ndarray._buf, self._buf, ndarray.size())

return ndarray

Expand Down Expand Up @@ -899,7 +896,7 @@ fn matrix[dtype: DType](object: NDArray[dtype]) raises -> Matrix[dtype]:
print(e)

var matrix = Matrix[dtype](shape=(object.ndshape[0], object.ndshape[1]))
memcpy(matrix._buf, object.data, matrix.size)
memcpy(matrix._buf, object._buf, matrix.size)

return matrix

Expand Down Expand Up @@ -988,21 +985,6 @@ fn fromstring[
return result^


fn _from_2darray[dtype: DType](array: NDArray[dtype]) raises -> Matrix[dtype]:
"""Create a matrix from an 2-darray.
[Unsafe] It simply uses the buffer of an ndarray.
It is useful when we want to solve a linear system. In this case, we treat
ndarray as a matrix. This simplify calculation and avoid too much check.
"""

var matrix = Matrix[dtype](shape=(array.ndshape[0], array.ndshape[1]))
matrix._buf = array.data

return matrix


# ===-----------------------------------------------------------------------===#
# Fucntions for arithmetic
# ===-----------------------------------------------------------------------===#
Expand Down

0 comments on commit a605942

Please sign in to comment.