tensor-operations 0.1.0

Week 13: Matrix and Tensor Operations in Rust - Manual vs Optimized
Documentation
+
��liU ��v�Rt^RIt^RIHtHt!RR4tRRltRRltR	R
lt	RRRllt
R
RltR#)zp
NumPy-based Matrix Operations Implementation
Demonstrates matrix operations using NumPy ndarray for comparison
N)�Tuple�Unionc��a�]tRt^
toRtV3RlRltV3RlRltV3RlRltV3RlR	ltV3R
lRlt	V3RlR
lt
V3RlRltV3RlRltV3RlRlt
V3RlRltV3RlRltV3RlRltV3RlRltV3RlRltV3RlRltV3R lR!ltV3R"lR#ltV3R$lR%ltV3R&lR'ltV3R(lR)ltV3R*lR+ltV3R,lR-ltV3R.lR/ltV3R0lR1ltV3R2lR3ltR4tVtR5#)6�NumpyMatrixzv
Matrix class using NumPy for efficient operations.
Provides the same interface as manual Matrix for easy comparison.
c�J<�V^8�dQhRS[S[PS[3,/#)��data)r�np�ndarray�list)�format�
__classdict__s"��=/Users/maksimvialykh/github/tensor-operations/numpy_matrix.py�__annotate__�NumpyMatrix.__annotate__s"���/�/�U�2�:�:�t�#3�4�/�c�r�\V\4'd-\P!V\PR7VnM+\P!V\PR7VnVP
P^8wd\R4hVP
PwVn
VnR#)z]
Initialize a matrix from NumPy array or list.

Args:
    data: NumPy array or list of lists
)�dtypezData must be 2-dimensionalN)�
isinstancerr	�array�float64r�asarray�ndim�
ValueError�shape�rows�cols)�selfrs&&r�__init__�NumpyMatrix.__init__so���d�D�!�!�����R�Z�Z�8�D�I��
�
�4�r�z�z�:�D�I��9�9�>�>�Q���9�:�:�#�y�y�����	�4�9rc� <�V^8�dQhRS[/#�r�return��str)rr
s"�rrr!s�����#�rc�,�\VP4#�z String representation of matrix.�r$r�rs&r�__repr__�NumpyMatrix.__repr__!����4�9�9�~�rc� <�V^8�dQhRS[/#r!r#)rr
s"�rrr%s������rc�,�\VP4#r&r'r(s&r�__str__�NumpyMatrix.__str__%r+rc�"<�V^8�dQhRRRR/#�r�otherrr"�)rr
s"�rrr)����3�3�]�3�}�3rc���VPVP8wgVPVP8wd\R4h\VPVP,4#)zElement-wise addition.z3Matrices must have the same dimensions for addition�rrrrr�rr2s&&r�__add__�NumpyMatrix.__add__)sE���9�9��
�
�"�d�i�i�5�:�:�&=��R�S�S��4�9�9�u�z�z�1�2�2rc�"<�V^8�dQhRRRR/#r1r3)rr
s"�rrr/r4rc���VPVP8wgVPVP8wd\R4h\VPVP,
4#)zElement-wise subtraction.z6Matrices must have the same dimensions for subtractionr6r7s&&r�__sub__�NumpyMatrix.__sub__/sE���9�9��
�
�"�d�i�i�5�:�:�&=��U�V�V��4�9�9�u�z�z�1�2�2rc�$<�V^8�dQhRS[RR/#�r�scalarr"r��float)rr
s"�rrr5s���/�/�e�/�
�/rc��\V\\34'g\R4h\	VP
V,4#)zScalar multiplication.zCan only multiply by scalar)r�intrB�	TypeErrorrr�rr@s&&r�__mul__�NumpyMatrix.__mul__5s3���&�3��,�/�/��9�:�:��4�9�9�v�-�.�.rc�$<�V^8�dQhRS[RR/#r?rA)rr
s"�rrr;s���$�$�u�$��$rc�$�VPV4#)z'Right multiplication (scalar * matrix).)rGrFs&&r�__rmul__�NumpyMatrix.__rmul__;s���|�|�F�#�#rc�"<�V^8�dQhRRRR/#r1r3)rr
s"�rrr?s���3�3�
�3�-�3rc��VPVP8wdA\RVPRVPRVPRVPR2	4h\VPVP,4#)zMatrix multiplication (A @ B).zCannot multiply matrices: (�xz) @ (�))rrrrrr7s&&r�
__matmul__�NumpyMatrix.__matmul__?sm���9�9��
�
�"����I�I�;�a��	�	�{�%��
�
�|�1�U�Z�Z�L��K��
��4�9�9�u�z�z�1�2�2rc�<�V^8�dQhRR/#�rr"rr3)rr
s"�rrrHs���(�(�=�(rc�@�\VPP4#)z#Return the transpose of the matrix.)rr�Tr(s&r�	transpose�NumpyMatrix.transposeHs���4�9�9�;�;�'�'rc� <�V^8�dQhRS[/#r!rA)rr
s"�rrrLs���/�/�U�/rc��VPVP8wd\R4h\\P
P
VP44#)zCalculate determinant.z,Determinant only defined for square matrices)rrrrBr	�linalg�detrr(s&r�determinant�NumpyMatrix.determinantLs:���9�9��	�	�!��K�L�L��R�Y�Y�]�]�4�9�9�-�.�.rc� <�V^8�dQhRS[/#r!��bool)rr
s"�rrrRs���&�&�4�&rc�4�VPVP8H#)zCheck if matrix is square.�rrr(s&r�	is_square�NumpyMatrix.is_squareRs���y�y�D�I�I�%�%rc� <�V^8�dQhRS[/#r!r`)rr
s"�rrrVs���/�/�t�/rc�j�VP4'gR#\VP44R8�#)zCheck if matrix is invertible.Fg�����|�=)rd�absr]r(s&r�
is_invertible�NumpyMatrix.is_invertibleVs+���~�~�����4�#�#�%�&��.�.rc�<�V^8�dQhRR/#rTr3)rr
s"�rrr\s���5�5��5rc��VP4'g\R4h\\PPVP44#)zCalculate matrix inverse.zMatrix is not invertible)rirrr	r[�invrr(s&r�inverse�NumpyMatrix.inverse\s9���!�!�#�#��7�8�8��2�9�9�=�=����3�4�4rc� <�V^8�dQhRS[/#r!rA)rr
s"�rrrbs���7�7��7rc�h�\\PPVPR44#)zCalculate Frobenius norm.�fro)rBr	r[�normrr(s&r�frobenius_norm�NumpyMatrix.frobenius_normbs ���R�Y�Y�^�^�D�I�I�u�5�6�6rc� <�V^8�dQhRS[/#r!rA)rr
s"�rrrfs���*�*�u�*rc��VP4'g\R4h\\P!VP
44#)zCalculate trace.z)Trace is only defined for square matrices)rdrrBr	�tracerr(s&rrx�NumpyMatrix.tracefs3���~�~����H�I�I��R�X�X�d�i�i�(�)�)rc�4<�V^8�dQhRS[P/#r!�r	r
)rr
s"�rrrls���,�,�R�Z�Z�,rc��VP4'g\R4h\PP	VP
4#)zCalculate eigenvalues.z,Eigenvalues only defined for square matrices)rdrr	r[�eigvalsrr(s&r�eigenvalues�NumpyMatrix.eigenvaluesls4���~�~����K�L�L��y�y� � ����+�+rc�H<�V^8�dQhRS[S[PR3,/#rT�rr	r
)rr
s"�rrrrs"���
6�
6�u�R�Z�Z��6�7�
6rc��VP4'g\R4h\PP	VP
4wrV\
V43#)zk
Calculate eigenvalues and eigenvectors.

Returns:
    Tuple of (eigenvalues, eigenvectors as NumpyMatrix)
z3Eigendecomposition only defined for square matrices)rdrr	r[�eigrr)rr~�eigenvectorss&  r�eigen�NumpyMatrix.eigenrsF���~�~����R�S�S�$&�I�I�M�M�$�)�)�$<�!���K��5�5�5rc� <�V^8�dQhRS[/#r!)rD)rr
s"�rrr~s���5�5�c�5rc�f�\\PPVP44#)zCalculate matrix rank.)rDr	r[�matrix_rankrr(s&r�rank�NumpyMatrix.rank~s ���2�9�9�(�(����3�4�4rc�.<�V^8�dQhRS[R,/#)rr")rr)r)rr
s"�rrr�s���.�.�%�(D�"E�.rc��\PPVP4wr\	V4\	V43#)zK
Calculate QR decomposition.

Returns:
    Tuple of (Q, R) where A = Q @ R
)r	r[�qrrr�r�Q�Rs&  r�qr_decomposition�NumpyMatrix.qr_decomposition�s/���y�y�|�|�D�I�I�&����1�~�{�1�~�-�-rc�J<�V^8�dQhRS[RS[PR3,/#rTr�)rr
s"�rrr�s$���2�2�U�=�"�*�*�m�C�D�2rc��\PPVPRR7wrp\	V4V\	V43#)zP
Singular Value Decomposition.

Returns:
    Tuple of (U, singular_values, V^T)
F)�
full_matrices)r	r[�svdrr)r�U�s�Vts&   rr��NumpyMatrix.svd�s7���9�9�=�=����%�=�@���b��1�~�q�+�b�/�1�1rc�<�V^8�dQhRR/#rTr3)rr
s"�rrr�s�����M�rc�*�VP4wrV#)z>Get orthonormal basis for column space using QR decomposition.)r�r�s&  r�column_space_basis�NumpyMatrix.column_space_basis�s���$�$�&����rc�<�V^8�dQhRR/#rTr3)rr
s"�rrr�s���H�H�-�Hrc��VP4wrpVP4pW4R1R3,PpVP^8�d\	V4#R#)z%Get orthonormal basis for null space.N:NNN)r�r�rV�sizer)rr�r�r�r��
null_spaces&     r�null_space_basis�NumpyMatrix.null_space_basis�sH���8�8�:���b��y�y�{����q��\�^�^�
�*4�/�/�A�*=�{�:�&�G�4�Grc�L<�V^8�dQhRS[RS[P3,RR/#�r�brr")rr	r
)rr
s"�rrr�s)���?�?�u�]�B�J�J�6�7�?�M�?rc���\V\4'dVPpM\P!V4p\\P
P
VPV44#)z�
Solve the linear system Ax = b.

Args:
    b: Right-hand side vector or matrix
    
Returns:
    Solution x such that A @ x = b
)rrrr	rr[�solve)rr��b_datas&& rr��NumpyMatrix.solve�sE���a��%�%��V�V�F��Z�Z��]�F��2�9�9�?�?�4�9�9�f�=�>�>rc�b<�V^8�dQhRS[RS[P3,RS[RS[3,/#r�)rr	r
rrB)rr
s"�rrr�s8���_�_�u�]�B�J�J�%>�?�_�E�-�Y^�J^�D_�_rc��\V\4'dVPpM\P!V4p\P
P
VPVRR7wr4rVVP^8�d)\\P
PV44MRpVP^8Xd\VPR^44V3#\V4V3#)z�
Solve least squares problem: minimize ||Ax - b||_2.

Args:
    b: Right-hand side vector or matrix
    
Returns:
    Tuple of (solution x, residual norm)
N)�rcondg�����)rrrr	rr[�lstsqr�rBrsr�reshape)rr�r�rO�	residualsr�r��
residual_norms&&      r�
least_squares�NumpyMatrix.least_squares�s����a��%�%��V�V�F��Z�Z��]�F� "�	�	����	�	�6��� N���d�<E�N�N�Q�<N��b�i�i�n�n�Y�7�8�TW�
�01���!��{�1�9�9�R��+�,�Q^�^�^��Q��Q^�^�^rc�8<�V^8�dQhRS[PRR/#)r�vectorr"rr{)rr
s"�rrr�s���/�/�2�:�:�/�-�/rc��\V4VP8wd&\R\V4RVPR24h\VPV,4#)z�
Demonstrate broadcasting: add vector to each row.

Args:
    vector: 1D array to add to each row
    
Returns:
    Result with vector added to each row
z
Vector size (z ) must match number of columns (rP)�lenrrrr)rr�s&&r�broadcasting_example� NumpyMatrix.broadcasting_example�sP���v�;�$�)�)�#��}�S��[�M�9Y�Z^�Zc�Zc�Yd�de�f�g�g��4�9�9�v�-�.�.r)rrrN) �__name__�
__module__�__qualname__�__firstlineno__�__doc__rr)r.r8r<rGrKrQrWr]rdrirnrtrxr~r�r�r�r�r�r�r�r�r��__static_attributes__�__classdictcell__)r
s@rrr
s�����
/�/�"����3�3�3�3�/�/�$�$�3�3�(�(�/�/�&�&�/�/�5�5�7�7�*�*�,�,�
6�
6�5�5�.�.�2�2���
H�H�?�?�"_�_�&/�/rrc�0�V^8�dQhR\R\/#�r�nr"�rDr)rs"rrr�s��"�"�3�"�;�"rc�@�\\P!V44#)z%Create identity matrix of size n x n.)rr	�eye)r�s&rr�r��s���r�v�v�a�y�!�!rc�<�V^8�dQhR\R\R\/#�rrrr"r�)rs"rrr�s!��/�/��/�3�/�;�/rc�B�\\P!W344#)z'Create zero matrix of size rows x cols.)rr	�zerosrcs&&rr�r��s���r�x�x���-�.�.rc�<�V^8�dQhR\R\R\/#r�r�)rs"rrr�s!��.�.�s�.�#�.�+�.rc�B�\\P!W344#)z*Create matrix of ones of size rows x cols.)rr	�onesrcs&&rr�r��s���r�w�w��|�,�-�-rc�H�V^8�dQhR\R\R\R\/#)rrr�seedr"r�)rs"rrr�s(��4�4��4�3�4�c�4�[�4rc��Ve \PPV4\\PP	W44#)zCreate random matrix.)r	�randomr�r�randn)rrr�s&&&r�
random_matrixr��s0����
�	�	���t���r�y�y���t�2�3�3rc�0�V^8�dQhR\R\/#r�r�)rs"rrr�s����c��k�rc���\P!W34p\V4F/p\V4FpRW#,^,,WV3&K	K1	\V4#)z4Create Hilbert matrix (ill-conditioned for testing).g�?)r	r��ranger)r��H�i�js&   r�hilbert_matrixr��sP��
���!���A�
�1�X���q��A��Q�U�Q�Y�'�A��d�G����q�>�r)N)r��numpyr	�typingrrrr�r�r�r�r�r3rr�<module>r�s8���
��I/�I/�X"�
/�
.�
4�r