本文整理汇总了Python中pycast.common.matrix.Matrix.initialize方法的典型用法代码示例。如果您正苦于以下问题:Python Matrix.initialize方法的具体用法?Python Matrix.initialize怎么用?Python Matrix.initialize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycast.common.matrix.Matrix
的用法示例。
在下文中一共展示了Matrix.initialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mul_type_error_test
# 需要导入模块: from pycast.common.matrix import Matrix [as 别名]
# 或者: from pycast.common.matrix.Matrix import initialize [as 别名]
def mul_type_error_test(self):
"""Test for TypeError, if Matrix is multiplied with a String
or a String is multiplied with a Matrix."""
rows = 3
cols = 2
data = [
[1, -2, 3],
[-4, 5, 6]
]
mtrx = Matrix(cols, rows)
mtrx.initialize(data, rowBased=False)
try:
mtrx * "Test"
except TypeError:
pass
else:
raise AssertionError # pragma: no cover
try:
"Test" * mtrx
except TypeError:
pass
else:
raise AssertionError # pragma: no cover
示例2: gauss_jordan_switch_column_test
# 需要导入模块: from pycast.common.matrix import Matrix [as 别名]
# 或者: from pycast.common.matrix.Matrix import initialize [as 别名]
def gauss_jordan_switch_column_test(self):
"""Test the gauss jordan algorithm if the first values is zero.
This test checks, if the lines are switched correctly.
"""
rows = 3
cols = 6
data = [
[0, 2, 0, 1, 0, 0],
[2, 3, 0, 0, 1, 0],
[3, 4, 1, 0, 0, 1]
]
mtrx = Matrix(cols, rows)
mtrx.initialize(data, rowBased=True)
exRes = [
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[-0.75, 0.5, 0.25],
[0.5, 0.0, -1.5],
[0.0, 0.0, 1.0]
]
res = mtrx.gauss_jordan()
self.assertEqual(res.matrix, exRes)
示例3: mul_matrix_test
# 需要导入模块: from pycast.common.matrix import Matrix [as 别名]
# 或者: from pycast.common.matrix.Matrix import initialize [as 别名]
def mul_matrix_test(self):
"""Test numeric multiplication on Matrices."""
# Only column of first matrix does match rows of second matrix
rows1 = 3
cols1 = 2
rows2 = 2
cols2 = 4
mtrx1 = Matrix(cols1, rows1)
mtrx2 = Matrix(cols2, rows2)
data1 = [
[1, -2],
[-4, 5],
[3, 6]
]
data2 = [
[3, 4, 5, 6],
[5, -6, 4, 5]
]
exRes = [
[-7, 13, 39],
[16, -46, -24],
[-3, 0, 39],
[-4, 1, 48]
]
mtrx1.initialize(data1, rowBased=True)
mtrx2.initialize(data2, rowBased=True)
res = mtrx1 * mtrx2
self.assertEqual(res.matrix, exRes)
示例4: matrix_string_representation_with_precision_test
# 需要导入模块: from pycast.common.matrix import Matrix [as 别名]
# 或者: from pycast.common.matrix.Matrix import initialize [as 别名]
def matrix_string_representation_with_precision_test(self):
"""Test if the precision is set correctly and used when printing a Matrix.
"""
size = 2
data = [
[1.0123343, -2.012341234123],
[3.04674567566, 4.012341234120]
]
mtrx = Matrix(size, size)
mtrx.initialize(data, rowBased=True)
mtrx.set_string_precision(4)
rep = mtrx.__str__()
# should print the number with 4 digits after decimal point
self.assertTrue(rep.find(" 3.0467 ") >= 0)
self.assertTrue(rep.find(" -2.0123") >= 0)
# but should not print the full number
self.assertFalse(rep.find(" 3.04674567566") >= 0)
self.assertFalse(rep.find(" -2.012341234123 ") >= 0)
# change precision
mtrx.set_string_precision(2)
rep = mtrx.__str__()
print mtrx
# should print the number with 2 digits after decimal point
# numbers should be rounded
self.assertTrue(rep.find(" 3.05 ") >= 0)
self.assertTrue(rep.find(" -2.01") >= 0)
示例5: matrix_to_multi_dim_timeseries_test
# 需要导入模块: from pycast.common.matrix import Matrix [as 别名]
# 或者: from pycast.common.matrix.Matrix import initialize [as 别名]
def matrix_to_multi_dim_timeseries_test(self):
"""Test to create a Timeseries from a Matrix."""
rows = 5
cols = 3
data = [
[2.4, 4.5, 6.1],
[3.6, 3.2, 9.4],
[5.6, 3.2, 8.7],
[4.3, 7.1, 3.3],
[7.2, 9.6, 0.3]
]
mtrx = Matrix(cols, rows)
mtrx.initialize(data, True)
ts = mtrx.to_multi_dim_timeseries()
tsData = [
[0, [2.4, 4.5, 6.1]],
[1, [3.6, 3.2, 9.4]],
[2, [5.6, 3.2, 8.7]],
[3, [4.3, 7.1, 3.3]],
[4, [7.2, 9.6, 0.3]]
]
exTs = MDTS.from_twodim_list(tsData, dimensions=3)
# expecting that TimeSeries.from_twodom_list() works properly
self.assertEqual(ts, exTs)
# Changing entries of the timeseries, should not affect matrix
row = 3
ts[row] = [row, 4, 3, 1]
for col in xrange(cols):
self.assertEqual(mtrx.get_value(col, row), data[row][col])
示例6: householder_test
# 需要导入模块: from pycast.common.matrix import Matrix [as 别名]
# 或者: from pycast.common.matrix.Matrix import initialize [as 别名]
def householder_test(self):
"""Test the householder transformation to get a matrix in bidiagonalization."""
# set up test
c = [
[4, 3, 0],
[2, 1, 2],
[4, 4, 0]
]
matrix = Matrix(3, 3)
matrix.initialize(c, rowBased=True)
# execute householder transformation
u, bidiag, v = matrix.householder()
# expect, that multiplication works correctly.
res = u * bidiag * v
# res should be equal with c (except some rounding errors)
for row in range(res.get_height()):
for col in range(res.get_width()):
self.assertAlmostEqual(res.get_value(col, row), c[row][col], PRECISION)
# bidiag matrix should have 0 values below the diagonal
self.assertAlmostEqual(bidiag.get_value(0, 1), 0, PRECISION)
self.assertAlmostEqual(bidiag.get_value(0, 2), 0, PRECISION)
self.assertAlmostEqual(bidiag.get_value(1, 2), 0, PRECISION)
self.assertAlmostEqual(bidiag.get_value(2, 0), 0, PRECISION)
示例7: svd_unitary_test
# 需要导入模块: from pycast.common.matrix import Matrix [as 别名]
# 或者: from pycast.common.matrix.Matrix import initialize [as 别名]
def svd_unitary_test(self):
"""Test if matrices u and v are unitary."""
a = [[22., 10., 2., 3., 7.],
[14., 7., 10., 0., 8.],
[-1., 13., - 1., -11., 3.],
[-3., -2., 13., -2., 4.],
[ 9., 8., 1., -2., 4.],
[ 9., 1., -7., 5., -1.],
[ 2., -6., 6., 5., 1.],
[ 4., 5., 0., -2., 2.]]
matrix = Matrix(5, 8)
matrix.initialize(a, rowBased=True)
u, diag, v = matrix.svd()
# u and v should be unitary matrices. Matrixmultiplication withs its
# transformation should be the identity Matrix.
res = u.transform() * u
res1 = v * v.transform()
for row in range(res.get_height()):
for col in range(res.get_width()):
if row == col:
# value should be 1 at diagonal
self.assertAlmostEqual(res.get_value(col, row), 1, PRECISION)
self.assertAlmostEqual(res1.get_value(col, row), 1, PRECISION)
else:
# value should be 0 otherwise.
self.assertAlmostEqual(res.get_value(col, row), 0, PRECISION)
self.assertAlmostEqual(res1.get_value(col, row), 0, PRECISION)
示例8: pseudoinverse_with_more_columns_test
# 需要导入模块: from pycast.common.matrix import Matrix [as 别名]
# 或者: from pycast.common.matrix.Matrix import initialize [as 别名]
def pseudoinverse_with_more_columns_test(self):
"""Test to calculate the pseudoinverse of a Matrix with more columns than rows."""
rows = 2
cols = 4
data = [
[-11, 2, -5.0, 7.0],
[ 2, -4, 3.4, 5.4]
]
mtrx = Matrix(cols, rows)
mtrx.initialize(data, rowBased=True)
# Expected result calculated with scipy
exRes = [
[-0.0541328, 0.02473614],
[ 0.00705413, -0.06480734],
[-0.02269591, 0.05255596],
[ 0.03956448, 0.09492743]
]
res = mtrx.pseudoinverse()
# Pseudoinverse of a m x n Matrix has to be a n x m Matrix
self.assertEqual(res.get_width(), rows)
self.assertEqual(res.get_height(), cols)
for row in range(cols):
for col in range(rows):
self.assertAlmostEqual(exRes[row][col], res.get_value(col, row))
示例9: sub_matrix_value_error_test
# 需要导入模块: from pycast.common.matrix import Matrix [as 别名]
# 或者: from pycast.common.matrix.Matrix import initialize [as 别名]
def sub_matrix_value_error_test(self):
"""Test for ValueError, when subtracting matrices of different size."""
rows1 = 2
cols1 = 3
rows2 = 3
cols2 = 3
data1 = [
[1, -2, 3],
[-4, 5, 6]
]
data2 = [
[2, 4, -3],
[5, -7, 3],
[5, -7, 3]
]
mtrx1 = Matrix(cols1, rows1)
mtrx2 = Matrix(cols2, rows2)
mtrx1.initialize(data1, rowBased=True)
mtrx2.initialize(data2, rowBased=True)
try:
mtrx1 - mtrx2
except ValueError:
pass
else:
raise AssertionError # pragma: no cover
示例10: set_string_precision_error_value_test
# 需要导入模块: from pycast.common.matrix import Matrix [as 别名]
# 或者: from pycast.common.matrix.Matrix import initialize [as 别名]
def set_string_precision_error_value_test(self):
"""Test for :py:exc:`ValueError` when trying to set the precision to a negative value."""
size = 2
data = [[1, 2], [3, 4]]
mtrx = Matrix(size, size)
mtrx.initialize(data, rowBased=True)
self.assertRaises(ValueError, mtrx.set_string_precision, -2)
示例11: matrix_string_representation_test
# 需要导入模块: from pycast.common.matrix import Matrix [as 别名]
# 或者: from pycast.common.matrix.Matrix import initialize [as 别名]
def matrix_string_representation_test(self):
"""Test the String representation of a Matrix instance."""
matrix = Matrix(2, 2)
a = [[1, 2], [-3, 4]]
matrix.initialize(a, rowBased=True)
rep = matrix.__str__()
self.assertTrue(rep.find(" 1.0") >= 0)
self.assertTrue(rep.find(" -3.0") >= 0)
# only one space before a negative number
self.assertFalse(rep.find(" -3.0") >= 0)
self.assertTrue(rep.find("Matrix") >= 0)
示例12: matrix_multiplication_square_test
# 需要导入模块: from pycast.common.matrix import Matrix [as 别名]
# 或者: from pycast.common.matrix.Matrix import initialize [as 别名]
def matrix_multiplication_square_test(self):
"""Test the matrix_multiplication with a square matrix"""
size = 33
array = [[1.0 for i in range(size)] for j in range(size)]
matrix = Matrix(size, size)
matrix2 = Matrix(size, size)
matrix.initialize(array, rowBased=True)
matrix2.initialize(array, rowBased=True)
result = matrix * matrix2
expRes = [[size for i in range(size)] for j in range(size)]
self.assertEqual(expRes, result.matrix)
示例13: invers_value_error_test
# 需要导入模块: from pycast.common.matrix import Matrix [as 别名]
# 或者: from pycast.common.matrix.Matrix import initialize [as 别名]
def invers_value_error_test(self):
"""Test if a :py:exc:`ValueError` is raised if Matrix is not regular"""
rows = 2
cols = 3
data = [
[1, 2, 3],
[4, 5, 6]
]
matrix = Matrix(cols, rows)
matrix.initialize(data, rowBased=True)
self.assertRaises(ValueError, matrix.invers)
示例14: gauss_jordan_value_error_test
# 需要导入模块: from pycast.common.matrix import Matrix [as 别名]
# 或者: from pycast.common.matrix.Matrix import initialize [as 别名]
def gauss_jordan_value_error_test(self):
"""Test for ValueError in gauss_jordan(), if matrix has wrong size."""
rows = 3
cols = 3
data = [
[0, 2, 0],
[2, 3, 0],
[3, 4, 1]
]
mtrx = Matrix(cols, rows)
mtrx.initialize(data, rowBased=False)
self.assertRaises(ValueError, mtrx.gauss_jordan)
示例15: gauss_jordan_non_singular_matrix_test
# 需要导入模块: from pycast.common.matrix import Matrix [as 别名]
# 或者: from pycast.common.matrix.Matrix import initialize [as 别名]
def gauss_jordan_non_singular_matrix_test(self):
"""Test for ValueError, if the Matrix is not invertible."""
rows = 3
cols = 6
data = [
[0, 2, 0, 1, 0, 0],
[0, 3, 0, 0, 1, 0],
[3, 4, 1, 0, 0, 1]
]
mtrx = Matrix(cols, rows)
mtrx.initialize(data, rowBased=True)
self.assertRaises(ValueError, mtrx.gauss_jordan)