Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyscan/general/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from .same_length import same_length
from .set_difference import set_difference
from .append_stack_or_contact import append_stack_or_contact
from .random_matrix import random_matrix
21 changes: 21 additions & 0 deletions pyscan/general/random_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import numpy as np


def random_matrix(rows, cols):
'''
Creates a matrix with random numbers between 0 and 1.

Parameters
----------
rows : int
Number of rows in the matrix.
cols : int
Number of columns in the matrix.

Returns
-------
numpy.ndarray
A matrix of shape (rows, cols) with random values between 0 and 1.
'''

return np.random.rand(rows, cols)
31 changes: 31 additions & 0 deletions test/test_general/test_random_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pyscan as ps
import numpy as np


def test_random_matrix_shape():
"""Test that random_matrix returns the correct shape"""
matrix = ps.random_matrix(1000, 2)
assert matrix.shape == (1000, 2), f"Expected shape (1000, 2), got {matrix.shape}"


def test_random_matrix_values_in_range():
"""Test that all values are between 0 and 1"""
matrix = ps.random_matrix(1000, 2)
assert np.all(matrix >= 0), "Some values are less than 0"
assert np.all(matrix <= 1), "Some values are greater than 1"


def test_random_matrix_different_sizes():
"""Test random_matrix with various sizes"""
for rows, cols in [(1, 1), (5, 3), (100, 50), (1000, 2)]:
matrix = ps.random_matrix(rows, cols)
assert matrix.shape == (rows, cols), f"Expected shape ({rows}, {cols}), got {matrix.shape}"
assert np.all(matrix >= 0) and np.all(matrix <= 1), "Values not in [0, 1] range"


def test_random_matrix_is_random():
"""Test that random_matrix produces different results on successive calls"""
matrix1 = ps.random_matrix(100, 100)
matrix2 = ps.random_matrix(100, 100)
# It's extremely unlikely that two random matrices would be identical
assert not np.array_equal(matrix1, matrix2), "Two successive calls produced identical matrices"