1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
|
.. _matrix:
Matrix
------
.. class:: Matrix
Syntax:
``mobj = h.Matrix(nrow, ncol)``
``mobj = h.Matrix(nrow, ncol, type)``
Description:
A class for manipulation of two dimensional arrays of numbers. A companion
to the :class:`Vector` class, Matrix contains routines for m*x=b, v1=m*v2, etc.
Individual element values are assigned and evaluated
using the syntax:
.. code-block::
python
m.getval(irow, icol)
m.setval(irow, icol, new_val)
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 .
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 <http://www.math.uiowa.edu/~dstewart/meschach/meschach.html>`_
(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.
----
.. data:: Matrix.x
Not currently supported in Python; use :meth:`Matrix.getval` and :meth:`Matrix.setval` instead.
----
.. method:: Matrix.nrow
Syntax:
``n = m.nrow()``
Description:
Returns the row dimension of the matrix. Row indices range from 0 to m.nrow()-1
.. note::
This method currently returns an integer, but prior to NEURON 7.6, it returned a float.
In older versions, it was thus sometimes necessary to cast the result to an int before
using e.g. range.
----
.. method:: Matrix.ncol
n = m.ncol()
Description:
returns the column dimension of the matrix. Column indices range
from 0 to m.ncol()-1
.. note::
This method currently returns an integer, but prior to NEURON 7.6, it returned a float.
In older versions, it was thus sometimes necessary to cast the result to an int before
using e.g. range.
----
.. method:: Matrix.resize
Syntax:
``mobj = m.resize(nrow, ncol)``
Description:
Change the size of the matrix. As many as possible of the former elements
are preserved. New elements are assigned the value of 0. New memory may
not have to be allocated depending on the size history of the matrix.
Example:
.. code-block::
python
>>> from neuron import h
>>> m = h.Matrix(3, 5)
>>> ignore_return = m.printf()
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
>>> for i in range(5):
... ignore_return = m.setcol(i, i)
...
>>> ignore_return = m.printf()
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
>>> ignore_return = m.resize(7, 7)
>>> ignore_return = m.printf()
0 1 2 3 4 0 0
0 1 2 3 4 0 0
0 1 2 3 4 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
>>> ignore_return = m.resize(4, 2)
>>> ignore_return = m.printf()
0 1
0 1
0 1
0 0
.. warning::
Implemented only for full matrices.
----
.. method:: Matrix.c
Syntax:
``mdest = msrc.c()``
Description:
Copy the matrix. msrc is unchanged.
.. warning::
Implemented only for full matrices.
----
.. method:: Matrix.bcopy
Syntax:
``mdest = msrc.bcopy(i0, j0, n, m [, mout])``
``mdest = msrc.bcopy(i0, j0, n, m, i1, j1 [, mout])``
Description:
Copy selected piece of a matrix. msrc is unchanged.
Copies the n x m submatrix with top-left (row i0, col j0) coordinates
to the corresponding submatrix of destination with top-left coordinates
(i1, j1). Out is resized if necessary.
Example:
.. code-block::
python
from neuron import h
m = h.Matrix(4,6)
for i in range(m.nrow()):
for j in range(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.
----
.. method:: Matrix.getval
Syntax:
``val = m.getval(irow, jcol)``
Description:
Returns the value of the matrix element. If m is sparse and the element
does not exist then 0 is returned without creating the element.
----
.. method:: Matrix.setval
Syntax:
``val = m.setval(irow, jcol, val)``
Description:
Sets the value of the matrix element. For sparse matrices, if the
element is 0, this method will create the element.
----
.. method:: Matrix.sprowlen
Syntax:
``n = m.sprowlen(i)``
Description:
Returns the number of existing(usually nonzero)
elements in the ith row of the sparse
matrix. Useful for iterating over a elements of a sparse matrix.
This function works only for sparse matrices.
See :meth:`Matrix.spgetrowval`
----
.. method:: Matrix.spgetrowval
Syntax:
``x = m.spgetrowval(i, jx, &j)``
Description:
Returns the existing element value and the column index (third pointer arg)
of the ith row and jx item. The latter ranges from 0 to m.sprowlen(i)-1
This function works only for sparse matrices (created with a third argument
of 2)
Example:
To print the elements of a sparse matrix.
.. code-block::
python
from __future__ import print_function
from neuron import h
def sparse_print(m):
m.printf()
print('m.nrow()', m.nrow())
for i in range(m.nrow()):
print("%d " % i, end='')
for jx in range(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)
----
.. method:: Matrix.printf
Syntax:
``0 = m.printf()``
``0 = m.printf("element_format")``
``0 = m.printf("element_format", "row_format")``
Description:
Print the matrix to the standard output with a default %-8g element format
and a default "\n" row format.
.. warning::
Needs a separate implementation for sparse and banded matrices. Prints sparse
as though it was full.
----
.. method:: Matrix.fprint
Syntax:
``0 = m.fprint(fileobj)``
``0 = m.fprint(fileobj, "element_format")``
``0 = m.fprint(fileobj, "element_format", "row_format")``
``0 = m.fprint(0, fileobj [,...])``
Description:
Same as :func:`printf` but prints to the File object (must be open for writing)
with a first line consisting of the two integers, nrow ncol.
Print the matrix to the open file object with a default %-8g element format
and a default "\n" row format.
Because of the "nrow ncol" first line, such a file can be read with :func:`scanf` .
If the first arg is a 0, then the nrow ncol pair of numbers will not
be printed.
.. warning::
Needs a separate implementation for sparse and banded matrices.
----
.. method:: Matrix.scanf
Syntax:
``0 = m.scanf(File_object)``
``0 = m.scanf(File_object, nrow, ncol)``
Description:
Read a file, including sizes, into a Matrix. The File_object is
an object of type :class:`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.
.. code-block::
python
from neuron import h
f = h.File("filename")
f.ropen()
m = h.Matrix()
m.scanf(f)
print('{} {}'.format(m.nrow(), m.ncol()))
.. warning::
Works only for full matrix types
.. seealso::
:meth:`Vector.scanf`, :func:`fscan`
----
.. method:: Matrix.mulv
Syntax:
``vobj = msrc.mulv(vin)``
``vobj = msrc.mulv(vin, vout)``
Description:
Multiplication of a Matrix by a Vector, vobj = msrc*vin.
Returns a new vector of dimension msrc.nrow. Optional Vector
vout is used for storage of the result. Vector
vin must have dimension msrc.ncol. vin and vout can be the same vector
if the matrix is square.
Example:
.. code-block::
python
from neuron import h
v1 = h.Vector(4)
v1.indgen(1,1)
m = h.Matrix(3, 4)
for i in range(3):
for j in range(3):
m.setval(i, j, i*10 + j)
.. code-block::
python
print("v1 {}".format(v1))
v1.printf()
print("m {}".format(m))
m.printf()
print("m * v1")
m.mulv(v1).printf()
A sparse example
.. code-block::
python
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.
----
.. method:: Matrix.getrow
Syntax:
``vobj = msrc.getrow(i)``
``vobj = msrc.getrow(i, vout)``
Description:
Return the i'th row of the matrix in a new :class:`Vector` (or use the storage
in the Vector vout if that arg is present). Range of i is from 0 to msrc.nrow-1.
.. warning::
Implemented only for full matrices.
----
.. method:: Matrix.getcol
Syntax:
``vobj = msrc.getcol(i)``
``vobj = msrc.getcol(i, vout)``
Description:
Return the i'th column of the matrix in a new vector (or use the storage
in vout if that arg is present). Range of i is from 0 to msrc.ncol-1.
.. warning::
Implemented only for full matrices.
----
.. method:: Matrix.getdiag
Syntax:
``vobj = msrc.getdiag(i)``
``vobj = msrc.getdiag(i, vout)``
Description:
Return the i'th diag of the matrix in a new vector (or use the storage
in vout if that arg is present) of size msrc.nrow.
Range is from -(msrc.nrow-1) to msrc.ncol-1
with 0 being the main diagonal, positive i refers to upper diagonals, and
negative i refers to lower diagonals. Upper diagonals fill the Vector
starting at position 0 and remaining elements are unused.
Lower diagonals fill the Vector ending at msrc.nrow-1 and the first
elements are unused.
Example:
.. code-block::
python
from __future__ import print_function
from neuron import h
m = h.Matrix(4,4)
for i in range(m.nrow()):
for j in range(m.ncol()):
m.setval(i, j, 1 + 10*j + 100*i)
m.printf()
for i in range(1 - m.nrow(), m.ncol()):
print("diagonal %d: " % i, end='')
print(list(m.getdiag(i))[max(0, -i) : (m.nrow() - i)])
.. warning::
Implemented only for full matrices.
----
.. method:: Matrix.solv
Syntax:
``vx = msrc.solv(vb)``
``vx = msrc.solv(vb, vout and/or 1 in either order)``
Description:
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:
.. code-block::
python
from neuron import h
b = h.Vector(3)
b.indgen(1,1)
m = h.Matrix(3, 3)
for i in range(m.nrow()):
for j in range(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[500] = 1
x = m.solv(b)
print()
x.printf("%8.3f", 475, 525)
b[500] = 0
b[499] = 1
print()
m.solv(b,1).printf("%8.3f", 475, 535)
.. warning::
Implemented only for full and sparse matrices.
----
.. method:: Matrix.det
Syntax:
``mantissa = m.det(_ref_base10exponent)``
Description:
Determinant of matrix m. Returns mantissa in range from -1 to 1 and
integer _ref_base10exponent[0].
Example:
.. code-block::
python
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])
----
.. method:: Matrix.mulm
Syntax:
``mobj = msrc.mulm(m)``
``mobj = msrc.mulm(m, mout)``
Description:
Multiplication of a Matrix by a Matrix, mobj = msrc*m. msrc and m are
unchanged. A new matrix is returned with size msrc.nrow x m.ncol.
msrc.ncol and m.nrow must be the same. If mout is present, that storage is
used for the result.
Example:
.. code-block::
python
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.
----
.. method:: Matrix.add
Syntax:
``mobj = m1srcdest.add(m2src)``
Description:
Return m1srcdest + m2src. The matrices must have the same rank.
This is one of those functions that modifies the source matrix (unless the
last optional mout arg is present) instead of
putting the result in a new destination matrix.
.. warning::
Implemented only for full matrices.
----
.. method:: Matrix.muls
Syntax:
``mobj = msrcdest.muls(scalar)``
Description:
Multiply the matrix by a scalar in place and return the matrix reference.
This is one of those functions that modifies the source matrix instead of
putting the result in a new destination matrix.
Example:
.. code-block::
python
m = h.Matrix(4,4)
m.ident()
m.muls(-10)
m.printf()
.. warning::
Implemented only for full and sparse matrices.
----
.. method:: Matrix.setrow
Syntax:
``mobj = msrcdest.setrow(i, vin)``
``mobj = msrcdest.setrow(i, scalar)``
Description:
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.
----
.. method:: Matrix.setcol
Syntax:
``mobj = msrcdest.setcol(i, vin)``
``mobj = msrcdest.setcol(i, scalar)``
Description:
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.
----
.. method:: Matrix.setdiag
Syntax:
``mobj = msrcdest.setdiag(i, vin)``
``mobj = msrcdest.setdiag(i, scalar)``
Description:
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:
.. code-block::
python
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.
----
.. method:: Matrix.zero
Syntax:
``mobj = msrcdest.zero()``
Description:
Fills the matrix with 0.
.. warning::
Implemented only for full matrices.
----
.. method:: Matrix.ident
Syntax:
``mobj = msrcdest.ident()``
Description:
Fills the principal diagonal with 1. All other elements are set to 0.
Example:
.. code-block::
python
m = h.Matrix(4, 6)
m.ident()
m.printf()
.. warning::
Implemented only for full matrices.
----
.. method:: Matrix.exp
Syntax:
``mobj = msrc.exp()``
``mobj = msrc.exp(mout)``
Description:
Returns a new matrix which is e^msrc. ie 1 + m + m*m/2 + m*m*m/6 + ...
Example:
.. code-block::
python
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.
----
.. method:: Matrix.pow
Syntax:
``mobj = msrc.pow(i)``
``mobj = msrc.pow(i, mout)``
Description:
Raise a matrix to a non-negative integer power.
Returns a new matrix which is msrc^i.
Example:
.. code-block::
python
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.
----
.. method:: Matrix.inverse
Syntax:
``mobj = msrc.inverse()``
``mobj = msrc.inverse(mout)``
Description:
Return 1/msrc in a new matrix. mobj*msrc = msrc*mobj = identity
Example:
.. code-block::
python
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.
----
.. method:: Matrix.svd
Syntax:
``dvec = msrc.svd()``
``dvec = msrc.svd(umat, vmat)``
Description:
Singular value decomposition of a rectangular n x m matrix.
On return ut*d*v = m where u is an orthogonal n x n matrix,
v is an orthogonal m x m matrix, and d is a diagonal n x m matrix
(represented as a vector) whose elements are non-negative and sorted
by decreasing value.
Note that if m*x = b then
vmat.mulv(x).mul(dvec) = umat.mulv(b)
Example:
.. code-block::
python
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(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.
----
.. method:: Matrix.transpose
Syntax:
``mdest = msrc.transpose()``
Description:
Return new matrix which is the transpose of the source matrix.
Example:
.. code-block::
python
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.
----
.. method:: Matrix.symmeig
Syntax:
``veigenvalues = msrc.symmeig(eigenvectors)``
Description:
Returns the eigenvalues and eigenvectors of a real symmetric matrix.
On exit the eigenvalues are returned in a new vector and the
eigenvectors are returned as an orthogonal matrix.
Note that the i'th column of the eigenvector matrix is the eigenvector
for the i'th element of the eigenvalue vector.
Example:
.. code-block::
python
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.
----
.. method:: Matrix.to_vector
Syntax:
``vobj = msrc.to_vector()``
``vobj = msrc.to_vector(vout)``
Description:
Copies the matrix elements into a :class:`Vector` in column order.
i.e the jth column starts
at vobj[msrc.nrow*j] .
The vector is sized to nrow*ncol.
Example:
.. code-block::
python
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.
----
.. method:: Matrix.from_vector
Syntax:
``mobj = msrcdest.from_vector(vec)``
Description:
Copies the vector elements into the matrix in column order. I.e
m[i][j] = v[j*nrow + i].
The size of vec must be equal to msrcdest.nrow()*msrcdest.ncol().
Example:
.. code-block::
python
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.
----
.. .. method:: Matrix.cholesky_factor
Syntax:
``mc = msrcdest.cholesky_factor()``
Description:
Cholesky factorization in place. msrcdest must be a symmetric positive
definite matrix. On return, it is a lower triangular matrix, L, such that
L*Ltranspose = msrc
.. warning::
Not implemented.
|