@@ -2505,39 +2505,62 @@ cdef class DenseMatrixBase(MatrixBase):
2505
2505
cdef DenseMatrixBase o = sympify(rhs)
2506
2506
if self .rows != o.rows:
2507
2507
raise ShapeError(" `self` and `rhs` must have the same number of rows." )
2508
- cdef DenseMatrixBase result = self .__class__ (self .rows, self .cols + o.cols)
2509
- symengine.row_join(deref(symengine.static_cast_DenseMatrix(self .thisptr)),
2510
- deref(symengine.static_cast_DenseMatrix(o.thisptr)),
2511
- deref(symengine.static_cast_DenseMatrix(result.thisptr)))
2512
- return result
2508
+ cdef DenseMatrixBase d = self .__class__ (self )
2509
+ deref(symengine.static_cast_DenseMatrix(d.thisptr)).row_join(deref(symengine.static_cast_DenseMatrix(o.thisptr)))
2510
+ return d
2513
2511
2514
2512
def col_join (self , bott ):
2515
2513
cdef DenseMatrixBase o = sympify(bott)
2516
2514
if self .cols != o.cols:
2517
2515
raise ShapeError(" `self` and `rhs` must have the same number of columns." )
2518
- cdef DenseMatrixBase result = self .__class__ (self .rows + o.rows, self .cols)
2519
- symengine.col_join(deref(symengine.static_cast_DenseMatrix(self .thisptr)),
2520
- deref(symengine.static_cast_DenseMatrix(o.thisptr)),
2521
- deref(symengine.static_cast_DenseMatrix(result.thisptr)))
2522
- return result
2516
+ cdef DenseMatrixBase d = self .__class__ (self )
2517
+ deref(symengine.static_cast_DenseMatrix(d.thisptr)).col_join(deref(symengine.static_cast_DenseMatrix(o.thisptr)))
2518
+ return d
2519
+
2520
+ def row_insert (self , pos , bott ):
2521
+ cdef DenseMatrixBase o = sympify(bott)
2522
+ if pos < 0 :
2523
+ pos = self .rows + pos
2524
+ if pos < 0 :
2525
+ pos = 0
2526
+ elif pos > self .rows:
2527
+ pos = self .rows
2528
+ if self .cols != o.cols:
2529
+ raise ShapeError(" `self` and `other` must have the same number of columns." )
2530
+ cdef DenseMatrixBase d = self .__class__ (self )
2531
+ deref(symengine.static_cast_DenseMatrix(d.thisptr)).row_insert(deref(symengine.static_cast_DenseMatrix(o.thisptr)), pos)
2532
+ return d
2533
+
2534
+ def col_insert (self , pos , bott ):
2535
+ cdef DenseMatrixBase o = sympify(bott)
2536
+ if pos < 0 :
2537
+ pos = self .cols + pos
2538
+ if pos < 0 :
2539
+ pos = 0
2540
+ elif pos > self .cols:
2541
+ pos = self .cols
2542
+ if self .rows != o.rows:
2543
+ raise ShapeError(" `self` and `other` must have the same number of rows." )
2544
+ cdef DenseMatrixBase d = self .__class__ (self )
2545
+ deref(symengine.static_cast_DenseMatrix(d.thisptr)).col_insert(deref(symengine.static_cast_DenseMatrix(o.thisptr)), pos)
2546
+ return d
2523
2547
2524
2548
def dot (self , b ):
2525
2549
cdef DenseMatrixBase o = sympify(b)
2526
2550
cdef DenseMatrixBase result = self .__class__ (self .rows, self .cols)
2527
- symengine.dot(deref(symengine.static_cast_DenseMatrix(self .thisptr)),
2528
- deref(symengine.static_cast_DenseMatrix(o.thisptr)),
2529
- deref(symengine.static_cast_DenseMatrix(result.thisptr)))
2530
- return result
2551
+ symengine.dot(deref(symengine.static_cast_DenseMatrix(self .thisptr)), deref(symengine.static_cast_DenseMatrix(o.thisptr)), deref(symengine.static_cast_DenseMatrix(result.thisptr)))
2552
+ if len (result) == 1 :
2553
+ return result[0 , 0 ]
2554
+ else :
2555
+ return result
2531
2556
2532
2557
def cross (self , b ):
2533
2558
cdef DenseMatrixBase o = sympify(b)
2534
2559
if self .cols * self .rows != 3 or o.cols * o.rows != 3 :
2535
2560
raise ShapeError(" Dimensions incorrect for cross product: %s x %s " %
2536
2561
((self .rows, self .cols), (b.rows, b.cols)))
2537
2562
cdef DenseMatrixBase result = self .__class__ (self .rows, self .cols)
2538
- symengine.cross(deref(symengine.static_cast_DenseMatrix(self .thisptr)),
2539
- deref(symengine.static_cast_DenseMatrix(o.thisptr)),
2540
- deref(symengine.static_cast_DenseMatrix(result.thisptr)))
2563
+ symengine.cross(deref(symengine.static_cast_DenseMatrix(self .thisptr)), deref(symengine.static_cast_DenseMatrix(o.thisptr)), deref(symengine.static_cast_DenseMatrix(result.thisptr)))
2541
2564
return result
2542
2565
2543
2566
@property
@@ -2548,6 +2571,10 @@ cdef class DenseMatrixBase(MatrixBase):
2548
2571
def cols (self ):
2549
2572
return self .ncols()
2550
2573
2574
+ @property
2575
+ def is_square (self ):
2576
+ return self .rows == self .cols
2577
+
2551
2578
def nrows (self ):
2552
2579
return deref(self .thisptr).nrows()
2553
2580
@@ -2596,6 +2623,12 @@ cdef class DenseMatrixBase(MatrixBase):
2596
2623
# No error checking is done
2597
2624
return c2py(deref(self .thisptr).get(i, j))
2598
2625
2626
+ def col (self , j ):
2627
+ return self [:, j]
2628
+
2629
+ def row (self , i ):
2630
+ return self [i, :]
2631
+
2599
2632
def set (self , i , j , e ):
2600
2633
i, j = self ._get_index(i, j)
2601
2634
return self ._set(i, j, e)
@@ -2672,6 +2705,13 @@ cdef class DenseMatrixBase(MatrixBase):
2672
2705
deref(out.thisptr).set(i, j, e_.thisptr)
2673
2706
return out
2674
2707
2708
+ def _applyfunc (self , f ):
2709
+ cdef int nr = self .nrows()
2710
+ cdef int nc = self .ncols()
2711
+ for i in range (nr):
2712
+ for j in range (nc):
2713
+ self ._set(i, j, f(self ._get(i, j)))
2714
+
2675
2715
def msubs (self , *args ):
2676
2716
cdef _DictBasic D = get_dict(* args)
2677
2717
return self .applyfunc(lambda x : x.msubs(D))
@@ -2833,6 +2873,9 @@ cdef class DenseMatrixBase(MatrixBase):
2833
2873
def tolist (self ):
2834
2874
return self [:]
2835
2875
2876
+ def _mat (self ):
2877
+ return self
2878
+
2836
2879
def atoms (self , *types ):
2837
2880
if types:
2838
2881
s = set ()
@@ -2871,40 +2914,43 @@ class DenseMatrixBaseIter(object):
2871
2914
cdef class MutableDenseMatrix(DenseMatrixBase):
2872
2915
2873
2916
def col_swap (self , i , j ):
2874
- symengine.column_exchange_dense(deref(symengine.static_cast_DenseMatrix(self .thisptr)),
2875
- i, j)
2917
+ symengine.column_exchange_dense(deref(symengine.static_cast_DenseMatrix(self .thisptr)), i, j)
2876
2918
2877
2919
def fill (self , value ):
2878
2920
for i in range (self .rows):
2879
2921
for j in range (self .cols):
2880
2922
self [i, j] = value
2881
2923
2882
2924
def row_swap (self , i , j ):
2883
- symengine.row_exchange_dense(deref(symengine.static_cast_DenseMatrix(self .thisptr)),
2884
- i, j)
2925
+ symengine.row_exchange_dense(deref(symengine.static_cast_DenseMatrix(self .thisptr)), i, j)
2926
+
2927
+ def rowmul (self , i , c , *args ):
2928
+ cdef Basic _c = sympify(c)
2929
+ symengine.row_mul_scalar_dense(deref(symengine.static_cast_DenseMatrix(self .thisptr)), i, _c.thisptr)
2930
+ return self
2931
+
2932
+ def rowadd (self , i , j , c , *args ):
2933
+ cdef Basic _c = sympify(c)
2934
+ symengine.row_add_row_dense(deref(symengine.static_cast_DenseMatrix(self .thisptr)), i, j, _c.thisptr)
2935
+ return self
2885
2936
2886
2937
def row_del (self , i ):
2887
2938
if i < - self .rows or i >= self .rows:
2888
2939
raise IndexError (" Index out of range: 'i = %s ', valid -%s <= i"
2889
2940
" < %s " % (i, self .rows, self .rows))
2890
2941
if i < 0 :
2891
2942
i += self .rows
2892
- symengine.row_del(deref(symengine.static_cast_DenseMatrix(self .thisptr)), i)
2943
+ deref(symengine.static_cast_DenseMatrix(self .thisptr)).row_del(i)
2944
+ return self
2893
2945
2894
2946
def col_del (self , i ):
2895
2947
if i < - self .cols or i >= self .cols:
2896
2948
raise IndexError (" Index out of range: 'i=%s ', valid -%s <= i < %s "
2897
2949
% (i, self .cols, self .cols))
2898
2950
if i < 0 :
2899
2951
i += self .cols
2900
- symengine.col_del(deref(symengine.static_cast_DenseMatrix(self .thisptr)), i)
2901
-
2902
- def _applyfunc (self , f ):
2903
- cdef int nr = self .nrows()
2904
- cdef int nc = self .ncols()
2905
- for i in range (nr):
2906
- for j in range (nc):
2907
- self ._set(i, j, f(self ._get(i, j)))
2952
+ deref(symengine.static_cast_DenseMatrix(self .thisptr)).col_del(i)
2953
+ return self
2908
2954
2909
2955
Matrix = DenseMatrix = MutableDenseMatrix
2910
2956
@@ -2913,12 +2959,6 @@ cdef class ImmutableDenseMatrix(DenseMatrixBase):
2913
2959
def __setitem__ (self , key , value ):
2914
2960
raise TypeError (" Cannot set values of {}" .format(self .__class__))
2915
2961
2916
- def set (self , i , j , e ):
2917
- raise TypeError (" Cannot set values of {}" .format(self .__class__))
2918
-
2919
- def _set (self , i , j , e ):
2920
- raise TypeError (" Cannot set values of {}" .format(self .__class__))
2921
-
2922
2962
ImmutableMatrix = ImmutableDenseMatrix
2923
2963
2924
2964
cdef matrix_to_vec(DenseMatrixBase d, symengine.vec_basic& v):
0 commit comments