Linear Algebra (linalg)#

The linalg module provides linear algebra functions for arrays.

Example#

import numpycore as np

# Matrix operations
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

# Matrix multiplication
C = A @ B  # or np.matmul(A, B)

# Matrix inverse
A_inv = np.linalg.inv(A)

# Eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(A)

# Solve linear system Ax = b
b = np.array([1, 2])
x = np.linalg.solve(A, b)

Matrix Decomposition#

Function

Description

Example

cholesky(a)

Cholesky decomposition

qr(a)

QR decomposition

svd(a)

Singular value decomposition

eig(a)

Eigenvalues and eigenvectors

eigh(a)

Eigenvalues for symmetric/Hermitian matrix

View

eigvals(a)

Eigenvalues only

eigvalsh(a)

Eigenvalues for symmetric/Hermitian

View

Matrix Operations#

Function

Description

Example

inv(a)

Matrix inverse

pinv(a)

Pseudo-inverse (Moore-Penrose)

det(a)

Matrix determinant

matrix_rank(a)

Matrix rank

trace(a)

Sum of diagonal elements

View

norm(x)

Matrix or vector norm

cond(x)

Condition number

matrix_power(a, n)

Matrix raised to power n

Solving Linear Systems#

Function

Description

Example

solve(a, b)

Solve linear system ax = b

lstsq(a, b)

Least-squares solution

tensorsolve(a, b)

Solve tensor equation

Products#

Function

Description

Example

dot(a, b)

Dot product

vdot(a, b)

Vector dot product

inner(a, b)

Inner product

outer(a, b)

Outer product

matmul(a, b)

Matrix multiplication

tensordot(a, b)

Tensor dot product

einsum(subscripts, …)

Einstein summation

cross(a, b)

Cross product

kron(a, b)

Kronecker product

Code Examples#

The following examples are extracted from the test suite.

eigh (test_phase1_linalg.py:26)
16        print(f"  NumpyCore result: {nc_result}")
17        sys.exit(1)
18    print(f"[OK] {test_name}: PASSED")
19
20def test_eigh_symmetric_2x2():
21    """Test eigh on 2x2 symmetric matrix"""
22    A_np = test_bind.np.array([[1.0, 2.0], [2.0, 1.0]])
23    A_nc = test_bind.numpycore.array([[1.0, 2.0], [2.0, 1.0]])
24
25    w_np, v_np = test_bind.np.linalg.eigh(A_np)
26    w_nc, v_nc = test_bind.numpycore.linalg.eigh(A_nc)
27
28    # Eigenvalues must match exactly
29    assert_arrays_equal(w_np, w_nc, "eigh_eigenvalues_2x2")
30
31    # Eigenvectors may differ by sign, compare absolute values
32    assert_arrays_equal(test_bind.np.abs(v_np), test_bind.np.abs(v_nc),
33                       "eigh_eigenvectors_2x2")
34
35def test_eigh_symmetric_3x3():
36    """Test eigh on 3x3 symmetric matrix"""
eigvalsh (test_phase1_linalg.py:57)
47    assert_arrays_equal(w_np, w_nc, "eigh_eigenvalues_3x3")
48    assert_arrays_equal(test_bind.np.abs(v_np), test_bind.np.abs(v_nc),
49                       "eigh_eigenvectors_3x3")
50
51def test_eigvalsh_symmetric_2x2():
52    """Test eigvalsh on 2x2 symmetric matrix"""
53    A_np = test_bind.np.array([[1.0, 2.0], [2.0, 1.0]])
54    A_nc = test_bind.numpycore.array([[1.0, 2.0], [2.0, 1.0]])
55
56    w_np = test_bind.np.linalg.eigvalsh(A_np)
57    w_nc = test_bind.numpycore.linalg.eigvalsh(A_nc)
58
59    assert_arrays_equal(w_np, w_nc, "eigvalsh_2x2")
60
61def test_eigvalsh_symmetric_100x100():
62    """Test eigvalsh on larger 100x100 symmetric matrix"""
63    # Create symmetric positive definite matrix
64    test_bind.np.random.seed(42)  # For reproducibility
65    A_np = test_bind.np.random.randn(100, 100)
66    A_np = A_np + A_np.T  # Make symmetric
67    A_nc = test_bind.numpycore.array(A_np.tolist())
trace (test_reduction_functions.py:292)
282def test_trace():
283    """Test np.linalg.trace() for diagonal sum"""
284    print("Testing np.linalg.trace()...")
285
286    # Test 1: Basic 2D array
287    nc_arr = test_bind.numpycore.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype='int32')
288    np_arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype='int32')
289
290    nc_trace = test_bind.numpycore.linalg.trace(nc_arr)
291    np_trace = np.trace(np_arr)
292    assert nc_trace == np_trace, f"trace() mismatch: {nc_trace} != {np_trace}"
293
294    # Test 2: With offset
295    nc_off1 = test_bind.numpycore.linalg.trace(nc_arr, offset=1)
296    np_off1 = np.trace(np_arr, offset=1)
297    assert nc_off1 == np_off1, f"trace() offset=1 mismatch: {nc_off1} != {np_off1}"
298
299    nc_off_neg = test_bind.numpycore.linalg.trace(nc_arr, offset=-1)
300    np_off_neg = np.trace(np_arr, offset=-1)