@@ -2505,32 +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
- cdef Basic e_
2510
- for i in range (self .rows):
2511
- for j in range (self .cols):
2512
- e_ = self ._get(i, j)
2513
- deref(result.thisptr).set(i, j, e_.thisptr)
2514
- for i in range (o.rows):
2515
- for j in range (o.cols):
2516
- e_ = sympify(o._get(i, j))
2517
- deref(result.thisptr).set(i, j + self .cols, e_.thisptr)
2518
- 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
2519
2511
2520
2512
def col_join (self , bott ):
2521
2513
cdef DenseMatrixBase o = sympify(bott)
2522
2514
if self .cols != o.cols:
2523
2515
raise ShapeError(" `self` and `rhs` must have the same number of columns." )
2524
- cdef DenseMatrixBase result = self .__class__ (self .rows + o.rows, self .cols)
2525
- cdef Basic e_
2526
- for i in range (self .rows):
2527
- for j in range (self .cols):
2528
- e_ = self ._get(i, j)
2529
- deref(result.thisptr).set(i, j, e_.thisptr)
2530
- for i in range (o.rows):
2531
- for j in range (o.cols):
2532
- e_ = sympify(o._get(i, j))
2533
- deref(result.thisptr).set(i + self .rows, j, e_.thisptr)
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
2547
+
2548
+ def dot (self , b ):
2549
+ cdef DenseMatrixBase o = sympify(b)
2550
+ cdef DenseMatrixBase result = self .__class__ (self .rows, self .cols)
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
2556
+
2557
+ def cross (self , b ):
2558
+ cdef DenseMatrixBase o = sympify(b)
2559
+ if self .cols * self .rows != 3 or o.cols * o.rows != 3 :
2560
+ raise ShapeError(" Dimensions incorrect for cross product: %s x %s " %
2561
+ ((self .rows, self .cols), (b.rows, b.cols)))
2562
+ cdef DenseMatrixBase result = self .__class__ (self .rows, self .cols)
2563
+ symengine.cross(deref(symengine.static_cast_DenseMatrix(self .thisptr)), deref(symengine.static_cast_DenseMatrix(o.thisptr)), deref(symengine.static_cast_DenseMatrix(result.thisptr)))
2534
2564
return result
2535
2565
2536
2566
@property
@@ -2541,6 +2571,10 @@ cdef class DenseMatrixBase(MatrixBase):
2541
2571
def cols (self ):
2542
2572
return self .ncols()
2543
2573
2574
+ @property
2575
+ def is_square (self ):
2576
+ return self .rows == self .cols
2577
+
2544
2578
def nrows (self ):
2545
2579
return deref(self .thisptr).nrows()
2546
2580
@@ -2589,6 +2623,12 @@ cdef class DenseMatrixBase(MatrixBase):
2589
2623
# No error checking is done
2590
2624
return c2py(deref(self .thisptr).get(i, j))
2591
2625
2626
+ def col (self , j ):
2627
+ return self [:, j]
2628
+
2629
+ def row (self , i ):
2630
+ return self [i, :]
2631
+
2592
2632
def set (self , i , j , e ):
2593
2633
i, j = self ._get_index(i, j)
2594
2634
return self ._set(i, j, e)
@@ -2665,6 +2705,13 @@ cdef class DenseMatrixBase(MatrixBase):
2665
2705
deref(out.thisptr).set(i, j, e_.thisptr)
2666
2706
return out
2667
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
+
2668
2715
def msubs (self , *args ):
2669
2716
cdef _DictBasic D = get_dict(* args)
2670
2717
return self .applyfunc(lambda x : x.msubs(D))
@@ -2826,6 +2873,9 @@ cdef class DenseMatrixBase(MatrixBase):
2826
2873
def tolist (self ):
2827
2874
return self [:]
2828
2875
2876
+ def _mat (self ):
2877
+ return self
2878
+
2829
2879
def atoms (self , *types ):
2830
2880
if types:
2831
2881
s = set ()
@@ -2864,24 +2914,43 @@ class DenseMatrixBaseIter(object):
2864
2914
cdef class MutableDenseMatrix(DenseMatrixBase):
2865
2915
2866
2916
def col_swap (self , i , j ):
2867
- for k in range (0 , self .rows):
2868
- self [k, i], self [k, j] = self [k, j], self [k, i]
2917
+ symengine.column_exchange_dense(deref(symengine.static_cast_DenseMatrix(self .thisptr)), i, j)
2869
2918
2870
2919
def fill (self , value ):
2871
2920
for i in range (self .rows):
2872
2921
for j in range (self .cols):
2873
2922
self [i, j] = value
2874
2923
2875
2924
def row_swap (self , i , j ):
2876
- for k in range (0 , self .cols):
2877
- self [i, k], self [j, k] = self [j, k], self [i, k]
2925
+ symengine.row_exchange_dense(deref(symengine.static_cast_DenseMatrix(self .thisptr)), i, j)
2878
2926
2879
- def _applyfunc (self , f ):
2880
- cdef int nr = self .nrows()
2881
- cdef int nc = self .ncols()
2882
- for i in range (nr):
2883
- for j in range (nc):
2884
- self ._set(i, j, f(self ._get(i, j)))
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
2936
+
2937
+ def row_del (self , i ):
2938
+ if i < - self .rows or i >= self .rows:
2939
+ raise IndexError (" Index out of range: 'i = %s ', valid -%s <= i"
2940
+ " < %s " % (i, self .rows, self .rows))
2941
+ if i < 0 :
2942
+ i += self .rows
2943
+ deref(symengine.static_cast_DenseMatrix(self .thisptr)).row_del(i)
2944
+ return self
2945
+
2946
+ def col_del (self , i ):
2947
+ if i < - self .cols or i >= self .cols:
2948
+ raise IndexError (" Index out of range: 'i=%s ', valid -%s <= i < %s "
2949
+ % (i, self .cols, self .cols))
2950
+ if i < 0 :
2951
+ i += self .cols
2952
+ deref(symengine.static_cast_DenseMatrix(self .thisptr)).col_del(i)
2953
+ return self
2885
2954
2886
2955
Matrix = DenseMatrix = MutableDenseMatrix
2887
2956
@@ -2890,12 +2959,6 @@ cdef class ImmutableDenseMatrix(DenseMatrixBase):
2890
2959
def __setitem__ (self , key , value ):
2891
2960
raise TypeError (" Cannot set values of {}" .format(self .__class__))
2892
2961
2893
- def set (self , i , j , e ):
2894
- raise TypeError (" Cannot set values of {}" .format(self .__class__))
2895
-
2896
- def _set (self , i , j , e ):
2897
- raise TypeError (" Cannot set values of {}" .format(self .__class__))
2898
-
2899
2962
ImmutableMatrix = ImmutableDenseMatrix
2900
2963
2901
2964
cdef matrix_to_vec(DenseMatrixBase d, symengine.vec_basic& v):
0 commit comments