mobj = h.Matrix(nrow, ncol)
mobj = h.Matrix(nrow, ncol, type)
A class for manipulation of two dimensional arrays of numbers. A companion to the Vector class, Matrix contains routines for m*x=b, v1=m*v2, etc.
Individual element values are assigned and evaluated using the syntax:
m.x[irow][icol]
which may appear anywhere in an expression or on the left hand side of an assignment statement. irow can range from 0 to m.nrow-1 and icol ranges from 0 to m.ncol-1 . (See x)
When possible, Matrix methods returning a Matrix use the form, mobj = m.f(args, [mout]), where mobj is a newly constructed matrix (m is unchanged) unless the optional last mout argument is present, in which case the return value is mout and mout is used to store the matrix. This style seems most efficient to me since many matrix operations cannot be done in-situ. Exceptions to this rule, eg m.zero(), are noted in the individual methods.
Similarly, Matrix methods returning a Vector use the form, vobj = m.f(args, [vout]), where vobj is a newly constructed Vector unless the optional last vout is present for storage of the vector elements. Use of vout is extremely useful in those cases where the vector is graphed since the result of the matrix operation does not invalidate the pointers to the vector elements.
Note that the return value allows these operations to be used as members of a filter chain or arguments to other functions.
By default, a new Matrix is of type MFULL (= 1) and allocates storage for all nrow*ncol elements. Scaffolding is in place for matrices of storage type MSPARSE (=2) and MBAND (=3) but not many methods have been interfaced to the meschach library at this time. If a method is called on a matrix type whose method has not been implemented, an error message will be printed. It is intended that implemented methods will be transparent to the user, eg m*x=b (x = m.solv(b) ) will solve the linear system regardless of the type of m and v1 = m*v2 (v1 = m.mulv(v2) ) will perform the vector multiplication.
Matrix is implemented using the meschach c library by David E. Stewart (discovered at http://www.netlib.org/c/index.html) which contains a large collection of routines for sparse, banded, and full matrices. Many of the useful routines have not been interfaced with the hoc interpreter but can be easily added on request or you can add it yourself by analogy with the code in nrn/src/ivoc/(matrix.c ocmatrix.[ch]) At this time the MFULL matrix type is complete enough to do useful work and MSPARSE can be used to multiply a matrix by a vector and solve Mx=b.
val = m.x[irow][icol]
m.x[irow][icol] = val
expr(m.x[irow][icol])
&m.x[irow][icol]
Individual element values are assigned and evaluated using the syntax:
m.x[irow][icol]
which may appear anywhere in an expression or on the left hand side of an assignment statement. irow can range from 0 to m.nrow-1 and icol ranges from 0 to m.ncol-1 .
For functions that require the address of a double value one may write
&m.x[irow][icol]
but one must be on guard for the case in which matrix storage is freed while another object holds a pointer to one of its elements. (Matrix does not currently notify the interpreter when storage has been freed.)
For sparse matrices an invocation of x[i][j] will create it if the element does not exist. Therefore if you wish to access every element use Matrix.getval() to avoid creating a very inefficient full matrix!
Example:
from neuron import h, gui m = h.Matrix(3,4) for i in range (int(m.nrow())): for j in range(int(m.ncol())): m.setval(i, j, 10*i + j) print i, j, m.getval(i, j) m.printf() h.xpanel("m") h.xvalue("m(1,3) interpret", "m.x[1][3]", 1, "m.printf") #xpvalue("m(1,3) address", &m.x[1][3], 1, "m.printf") h.xpanel()
Warning
When dealing with sparse matrices, be careful when using the m.x[][] notation since the mere act of evaluating a zero element will create it if it does not exist. In this case it is better to use the getval() function.
In Python, the m.x[i][j] syntax does not work and one must use the setval() function
Example:
from neuron import h m = h.Matrix(3, 5) m.printf() print for i in xrange(5): m.setcol(i,i) m.printf() print m.resize(7,7) m.printf() print m.resize(4,2) m.printf()
Warning
Implemented only for full matrices.
Warning
Implemented only for full matrices.
mdest = msrc.bcopy(i0, j0, n, m [, mout])
mdest = msrc.bcopy(i0, j0, n, m, i1, j1 [, mout])
Example:
python from neuron import h m = h.Matrix(4,6) for i in range(0, int(m.nrow())): for j in range(0, int(m.ncol())): m.setval(i, j, 1 + 10*i+j) m.printf() print m.bcopy(1,2,2,3).printf() print m.bcopy(1,2,2,3,2,3).printf() print m.bcopy(1,2,2,3,2,3, h.Matrix(8,8)).printf()
Warning
Implemented only for full matrices.
To print the elements of a sparse matrix.
from __future__ import print_function
from neuron import h
def sparse_print(m):
m.printf()
print('m.nrow()', m.nrow())
for i in range(int(m.nrow())):
print("%d " % i, end='')
for jx in range(int(m.sprowlen(i))):
j = h.ref(0)
x=m.spgetrowval(i, jx, j)
print(" %d:%f" % (j[0], x), end='')
print()
m = h.Matrix(4, 5, 2)
m.setval(0, 2, 1.2)
m.setval(0, 4, 2.4)
m.setval(1, 1, 3.1)
for i in range(4):
m.setval(3, i, i/10.)
sparse_print(m)
0 = m.printf()
0 = m.printf("element_format")
0 = m.printf("element_format", "row_format")
Warning
Needs a separate implementation for sparse and banded matrices. Prints sparse as though it was full.
0 = m.fprint(fileobj)
0 = m.fprint(fileobj, "element_format")
0 = m.fprint(fileobj, "element_format", "row_format")
0 = m.fprint(0, fileobj [,...])
Warning
Needs a separate implementation for sparse and banded matrices.
0 = m.scanf(File_object)
0 = m.scanf(File_object, nrow, ncol)
Read a file, including sizes, into a Matrix. The File_object is an object of type File and must be opened for reading prior to the scanf. If nrow,ncol arguments are not present, the first two numbers in the file must be nrow and mcol respectively. In either case those values are used to resize the matrix. The following nrow*mcol numbers are row streams, eg it is often natural to have one row on a single line or else to organize the file as a list of row vectors with only one number per line. Strings in the file that cannot be parsed as numbers are ignored.
from neuron import h
f = h.File("filename")
f.ropen()
m = h.Matrix()
m.scanf(f)
print m.nrow(), m.ncol()
Warning
Works only for full matrix types
See also
vobj = msrc.mulv(vin)
vobj = msrc.mulv(vin, vout)
print "v1", v1
v1.printf()
print "m", m
m.printf()
print "m*v1"
m.mulv(v1).printf()
A sparse example
from neuron import h
v1 = h.Vector(100)
v1.indgen(1,1)
m = h.Matrix(100, 100, 2) ##sparse matrix
##reverse permutation
for i in range(100):
m.setval(i, 99 - i, 1)
m.mulv(v1).printf()
Warning
Implemented only for full and sparse matrices.
vobj = msrc.getrow(i)
vobj = msrc.getrow(i, vout)
Warning
Implemented only for full matrices.
vobj = msrc.getcol(i)
vobj = msrc.getcol(i, vout)
Warning
Implemented only for full matrices.
vobj = msrc.getdiag(i)
vobj = msrc.getdiag(i, vout)
Example:
from __future__ import print_function from neuron import h m = h.Matrix(4,4) for i in range(int(m.nrow())): for j in range (int(m.ncol())): m.setval(i, j, 1 + 10*j + 100*i) m.printf() for i in range(int(1-m.nrow()), int(m.ncol())): print("diagonal %d: " % i, end='') print(list(m.getdiag(i))[max(0, -i) : int(m.nrow() - i)])
Warning
Implemented only for full matrices.
vx = msrc.solv(vb)
vx = msrc.solv(vb, vout and/or 1 in either order)
Solves the linear system msrc*vx = vb by LU factorization. msrc must be a square matrix and vb must have size equal to msrc.nrow. The answer will be returned in a new Vector of size msrc.nrow. msrc is not changed. The LU factorization is stored in case it is desired for later reuse with a different vb. Re-use of the LU factorization will actually take place only if the second or third argument is 1 and msrc has not changed in size.
Note: if the LUfactor is used, changes to the actual values of msrc would not affect the solution on subsequent calls to solv.
Example:
from neuron import h b = h.Vector(3) b.indgen(1,1) m = h.Matrix(3, 3) for i in range(int(m.nrow())): for j in range(int(m.ncol())): m.setval(i, j, i*j + 1) print "b" b.printf() print "m" m.printf() print print "solution of m*x = b" print m.solv(b).printf() .. code-block:: python m = h.Matrix(1000, 1000, 2) ## sparse type m.setdiag(0, 3) m.setdiag(-1, -1) m.setdiag(1, -1) b = h.Vector(1000) b.x[500] = 1 x = m.solv(b) print x.printf("%8.3f", 475, 525) b.x[500] = 0 b.x[499] = 1 print m.solv(b,1).printf("%8.3f", 475, 535)
Warning
Implemented only for full and sparse matrices.
Example:
from neuron import h m = h.Matrix(2,2) m.setval(0, 1, 20) m.setval(1, 0, 30) m.printf() ex = h.ref(0) mant = m.det(ex) print mant*10**ex[0]
mobj = msrc.mulm(m)
mobj = msrc.mulm(m, mout)
Example:
from neuron import h
m1 = h.Matrix(6, 6)
for i in range(-1, 2):
if i == 0:
m1.setdiag(i, 2)
else:
m1.setdiag(i, -1)
m2 = m1.inverse()
print "m1"
m1.printf()
print "m2"
m2.printf(" %8.5f")
print "m1*m2"
m1.mulm(m2).printf(" %8.5f")
Warning
Implemented only for full matrices.
Warning
Implemented only for full matrices.
Example:
m = h.Matrix(4,4) m.ident() m.muls(-10) m.printf()
Warning
Implemented only for full and sparse matrices.
mobj = msrcdest.setrow(i, vin)
mobj = msrcdest.setrow(i, scalar)
Fill the ith row of the msrcdest matrix with the values of the Vector vin. The vector must have size msrcdest.ncol
Otherwise fill the matrix row with a constant.
Warning
Implemented only for full matrices and sparse.
mobj = msrcdest.setcol(i, vin)
mobj = msrcdest.setcol(i, scalar)
Fill the ith column of the msrcdest matrix with the values of the Vector vin. The vector must have size msrcdest.mrow
Otherwise fill the matrix column with a constant.
Warning
Implemented only for full matrices.
mobj = msrcdest.setdiag(i, vin)
mobj = msrcdest.setdiag(i, scalar)
Fill the ith diagonal of the msrcdest matrix with the values of the Vector vin. The vector must have size msrcdest.mrow. The ith diagonal ranges from -(mrow-1) to mcol-1. For positive diagonals, the starting position of vector elements is 0 and trailing elements are ignored. For negative diagonals, the ending position of the vector elements is nrow-1 and beginning elements are ignored.
Otherwise fill the matrix diagonal with a constant.
Example:
from neuron import h m = h.Matrix(5,7) v1 = h.Vector(5) for i in range(-4,7): m.setdiag(i, i) m.printf() print for i in range (-4,7): v1.indgen(1,1) m.setdiag(i, v1) m.printf()
Warning
Implemented only for full and sparse matrices.
Warning
Implemented only for full matrices.
Example:
m = h.Matrix(4,6) m.ident() m.printf()
Warning
Implemented only for full matrices.
mobj = msrc.exp()
mobj = msrc.exp(mout)
Example:
from neuron import h m = h.Matrix(8,8) v1 = h.Vector(8) for i in range(-1,2): v1.fill(2 - 3*abs(i)) m.setdiag(i, v1) m.exp().printf()
Warning
Implemented only for full matrices. But doesn't really make sense for any other type since the result would normally be full.
mobj = msrc.pow(i)
mobj = msrc.pow(i, mout)
Example:
from neuron import h m = h.Matrix(6, 6) m.ident() m.setval(0, 5, 1) m.setval(5, 0, 1) for i in range(6): print i m.pow(i).printf()
Warning
Implemented only for full matrices. But doesn't really make sense for any other type since the result would normally be full.
mobj = msrc.inverse()
mobj = msrc.inverse(mout)
Example:
from neuron import h m = h.Matrix(7,7) v1 = h.Vector(7) for i in range(-1, 2): v1.fill(2 - 3*abs(i)) m.setdiag(i, v1) minv = m.inverse() print m.printf() print minv.printf() print m.mulm(minv).printf()
Warning
Implemented only for full matrices. But doesn't really make sense for any other type since the result would normally be full.
dvec = msrc.svd()
dvec = msrc.svd(umat, vmat)
Example:
from neuron import h def svdtest(a): umat = h.Matrix() vmat = h.Matrix() dvec = a.svd(umat, vmat) dmat = h.Matrix(a.nrow(), a.ncol()) dmat.setdiag(0, dvec) print "dvec" dvec.printf() print "dmat" dmat.printf() print "umat" umat.printf() print "vmat" vmat.printf() print "input " a.printf() print "ut*d*v" umat.transpose().mulm(dmat).mulm(vmat).printf() a = h.Matrix(5, 3) a.setdiag(0, a.getdiag(0).indgen().add(1)) svdtest(a) a = h.Matrix(6, 6) r = h.Random() r.discunif(1,10) for i in range(int(a.nrow())): a.setrow(i, a.getrow(i).setrand(r)) svdtest(a) a = h.Matrix(2,2) a.setrow(0, 1) a.setrow(1, 2) svdtest(a)
Warning
Implemented only for full matrices. umat and vmat are also full.
Example:
from neuron import h m = h.Matrix(1,5) for i in range(5): m.setval(0, i, i) m.printf() print m.transpose().printf() print m.transpose().mulm(m).printf() print m.mulm(m.transpose()).printf()
Warning
Implemented only for full matrices.
Example:
from neuron import h m = h.Matrix(5,5) m.setdiag(0, 2) m.setdiag(-1, -1) m.setdiag(1, -1) m.printf() q = h.Matrix(1,1) e = m.symmeig(q) print "eigenvectors" q.printf() print print "eigenvalues" e.printf() print print "qt*m*q" q.transpose().mulm(m).mulm(q).printf() print print "qt*q" q.transpose().mulm(q).printf()
Warning
Implemented only for full matrices.
msrc must be symmetric but that fact is not checked.
vobj = msrc.to_vector()
vobj = msrc.to_vector(vout)
Example:
from neuron import h m = h.Matrix(4,5) m.from_vector(m.to_vector().indgen()).printf()
Warning
Works for sparse matrices but the output vector will still be size nrow*ncol. Not very efficient since vobj and msrc do not share memory.
Example:
from neuron import h m = h.Matrix(4,5) m.from_vector(m.to_vector().indgen()).printf()
Warning
Works for sparse matrices but all elements will exist so not really sparse.