Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 39d662ae authored by hhakim's avatar hhakim
Browse files

Rename Faust.todense() to Faust.toarray() and add a new Faust.todense() which...

Rename Faust.todense() to Faust.toarray() and add a new Faust.todense() which returns a numpy.matrix.

The goal is to respect the way scipy works with its two functions toarray() and todense() (resp. returning a numpy.ndarray and a numpy.matrix).
parent ef586bb2
No related branches found
No related tags found
No related merge requests found
......@@ -24,13 +24,13 @@ class TestFaustPy(unittest.TestCase):
for i in range(0, num_factors):
d1, d2 = d2, r.randint(1, TestFaustPy.MAX_DIM_SIZE)
factors += [sparse.random(d1, d2, density=0.1, format='csr',
dtype=np.float64)] #.todense() removed
dtype=np.float64)] #.toarray() removed
#print("factor",i,":", factors[i])
self.F = Faust(factors)
self.factors = factors
# we keep dense matrices as reference for the tests
for i in range(0, num_factors):
self.factors[i] = factors[i].todense()
self.factors[i] = factors[i].toarray()
print("Tests on random Faust with dims=", self.F.shape[0],
self.F.shape[1])
print("Num. factors:", num_factors)
......@@ -186,9 +186,9 @@ class TestFaustPy(unittest.TestCase):
def testToDense(self):
print("testToDense()")
prod = self.mulFactors()
test_prod = self.F.todense()
test_prod = self.F.toarray()
self.assertProdEq(prod, test_prod)
#self.assertTrue((self.F.todense() == prod).all())
#self.assertTrue((self.F.toarray() == prod).all())
def testMul(self):
......@@ -203,10 +203,10 @@ class TestFaustPy(unittest.TestCase):
def testTranspose(self):
print("testTranspose()")
tFaust = self.F.transpose()
tF = tFaust.todense()
tF = tFaust.toarray()
del tFaust # just to test weak reference of underlying Faust::Transform
# works (otherwise it should crash with core dump later)
F = self.F.todense() # to avoid slowness
F = self.F.toarray() # to avoid slowness
for i in range(0, tF.shape[0]):
for j in range(0, tF.shape[1]):
if(F[j,i] != 0):
......@@ -225,7 +225,7 @@ class TestFaustPy(unittest.TestCase):
#print(self.F.shape[0], self.F.shape[1])
self.assertEqual(tF2.shape[1], tF.shape[1])
self.assertEqual(tF2.shape[0], tF.shape[0])
tF2 = tF2.todense()
tF2 = tF2.toarray()
for i in range(0, tF.shape[0]):
for j in range(0, tF.shape[1]):
if(F[j,i] != 0):
......@@ -252,14 +252,14 @@ class TestFaustPy(unittest.TestCase):
def testConjugate(self):
print("Test Faust.conj()")
test_Fc = self.F.conj().todense()
ref_Fc = self.F.todense().conj()
test_Fc = self.F.conj().toarray()
ref_Fc = self.F.toarray().conj()
self.assertTrue((test_Fc == ref_Fc).all())
def testGetH(self):
print("Test Faust.getH()")
test_Fct = self.F.getH().todense()
ref_Fct = self.F.todense().conj().T
test_Fct = self.F.getH().toarray()
ref_Fct = self.F.toarray().conj().T
#print("test_Fct=", test_Fct)
#print("ref_Fct=", ref_Fct)
ref_Fct[ref_Fct==0] = 1
......@@ -293,7 +293,7 @@ class TestFaustPyCplx(TestFaustPy):
# we keep dense matrices as reference for the tests
for i in range(0, num_factors):
if(not isinstance(factors[i], np.ndarray)):
self.factors[i] = factors[i].todense()
self.factors[i] = factors[i].toarray()
print("Tests on random complex Faust with dims=", self.F.shape[0],
self.F.shape[1])
print("Num. factors:", num_factors)
......
......@@ -131,7 +131,7 @@ print("Ok")
print("*** CONVERSION DENSE MATRIX ***")
F_dense_expected=faust_multiply(list_factor,np.eye(dim2))
F_dense=F.todense()
F_dense=F.toarray()
if not (F_dense.shape == (dim1, dim2)) :
print("expected size : "+str([dim1, dim2]))
......@@ -142,7 +142,7 @@ if not (F_dense==F_dense_expected).all():
F_trans_dense=F_trans.todense()
F_trans_dense=F_trans.toarray()
if not (F_trans_dense.shape == (dim2, dim1)) :
print("expected size : "+str([dim2, dim1]))
print("got : "+str(F_dense_trans.shape))
......@@ -151,7 +151,7 @@ if not (np.transpose(F_dense)==F_trans_dense).all():
raise ValueError('invalid value')
F_trans_trans_dense=F_trans_trans.todense()
F_trans_trans_dense=F_trans_trans.toarray()
if not (F_trans_trans_dense.shape == (dim1, dim2)) :
print("expected size : "+str([dim1, dim2]))
......
......@@ -120,7 +120,7 @@ for j in range(nb_dim):
if result_comparison:
list_factor_sp[i]=np.floor(list_factor_sp[i])
list_factor_dense[i]=list_factor_sp[i].todense()
list_factor_dense[i]=list_factor_sp[i].toarray()
#print(list_factor_dense[i])
......@@ -134,7 +134,7 @@ for j in range(nb_dim):
F=FaustPy.Faust(list_factor_dense)
F_dense=F.todense()
F_dense=F.toarray()
if(F.shape[0] != dim) or (F.shape[1] != dim):
raise ValueError('invalid Faust size')
......
......@@ -70,7 +70,7 @@ class Faust:
note that not all are.
You have the capability to retrieve the dense matrix with the
method Faust.todense but it will cost the multiplication of the Faust's factors.
method Faust.toarray but it will cost the multiplication of the Faust's factors.
It's noteworthy that in this documentation the expression 'dense matrix'
designates the numpy dense matrix corresponding to a Faust, that is the
matrix obtained by the multiplication of the
......@@ -286,7 +286,7 @@ class Faust:
>>> H1 = F.getH()
>>> H2 = F.transpose()
>>> H2 = H2.conj()
>>> (H1.todense() == H2.todense()).all()
>>> (H1.toarray() == H2.toarray()).all()
True
<b/> See also Faust.transpose, Faust.conj
......@@ -381,19 +381,31 @@ class Faust:
"""
return F.m_faust.multiply(A)
def todense(F):
def toarray(F):
"""
Converts the current Faust into a numpy matrix.
Converts the current Faust into a numpy array.
WARNING: this function costs F.get_num_factors()-1 matrix multiplications.
Returns:
A numpy matrix.
A numpy ndarray.
"""
identity = np.eye(F.shape[1], F.shape[1])
F_dense = F*identity
return F_dense
def todense(F):
"""
Converts the current Faust into a numpy matrix.
WARNING: this function costs F.get_num_factors()-1 matrix multiplications.
Returns:
A numpy matrix.
"""
return np.matrix(F.toarray())
def __getitem__(F, indices):
"""
Gets a submatrix of the full matrix of F.
......@@ -619,7 +631,7 @@ class Faust:
>>> F.save("F.mat")
>>> G = Faust(filepath="F.mat")
>>> H = Faust(filepath="F.mat", scale=2)
>>> (H.todense()/G.todense() != 2).any()
>>> (H.toarray()/G.toarray() != 2).any()
False
<b/> See also Faust.__init__.
......
......@@ -54,9 +54,9 @@ list_factor=[0]*nb_factor
int_max = 120
density_per_fact=0.1;
list_factor_sparse[0]=int_max*sp.random(dim1,dim1,density=density_per_fact,format='csr',dtype=np.float64);
list_factor[0]=list_factor_sparse[0].todense();
list_factor[0]=list_factor_sparse[0].toarray();
list_factor_sparse[1]=int_max*sp.random(dim1,dim2,density=density_per_fact,format='csr',dtype=np.float64);
list_factor[1]=list_factor_sparse[1].todense();
list_factor[1]=list_factor_sparse[1].toarray();
#print(list_factor[0])
......@@ -77,7 +77,7 @@ x = np.random.randint(int_max, size=(dim2,1))
y = A * x
# convert a faust to numpy matrix
A_numpy = A.todense()
A_numpy = A.toarray()
# slicing
coeff = A[0,0]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment