Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

""" 

Fundamental arithmetic of dense matrices. The dense matrix is stored 

as a list of lists. 

 

""" 

 

from sympy.core.compatibility import range 

 

def add(matlist1, matlist2, K): 

    """ 

    Adds matrices row-wise. 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densearith import add 

    >>> from sympy import ZZ 

    >>> e = [ 

    ... [ZZ(12), ZZ(78)], 

    ... [ZZ(56), ZZ(79)]] 

    >>> f = [ 

    ... [ZZ(1), ZZ(2)], 

    ... [ZZ(3), ZZ(4)]] 

    >>> g = [ 

    ... [ZZ.zero, ZZ.zero], 

    ... [ZZ.zero, ZZ.zero]] 

    >>> add(e, f, ZZ) 

    [[13, 80], [59, 83]] 

    >>> add(f, g, ZZ) 

    [[1, 2], [3, 4]] 

 

    See Also 

    ======== 

 

    addrow 

    """ 

    return [addrow(row1, row2, K) for row1, row2 in zip(matlist1, matlist2)] 

 

def addrow(row1, row2, K): 

    """ 

    Adds two rows of a matrix element-wise. 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densearith import addrow 

    >>> from sympy import ZZ 

 

    >>> a = [ZZ(12), ZZ(34), ZZ(56)] 

    >>> b = [ZZ(14), ZZ(56), ZZ(63)] 

    >>> c = [ZZ(0), ZZ(0), ZZ(0)] 

 

    >>> addrow(a, b, ZZ) 

    [26, 90, 119] 

    >>> addrow(b, c, ZZ) 

    [14, 56, 63] 

 

    """ 

    return [element1 + element2 for element1, element2 in zip(row1, row2)] 

 

 

def sub(matlist1, matlist2, K): 

    """ 

    Subtracts two matrices by first negating the second matrix and 

    then adding it to first matrix. 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densearith import sub 

    >>> from sympy import ZZ 

    >>> e = [ 

    ... [ZZ(12), ZZ(78)], 

    ... [ZZ(56), ZZ(79)]] 

    >>> f = [ 

    ... [ZZ(1), ZZ(2)], 

    ... [ZZ(3), ZZ(4)]] 

    >>> g = [ 

    ... [ZZ.zero, ZZ.zero], 

    ... [ZZ.zero, ZZ.zero]] 

    >>> sub(e, f, ZZ) 

    [[11, 76], [53, 75]] 

    >>> sub(f, g, ZZ) 

    [[1, 2], [3, 4]] 

 

    See Also 

    ======== 

 

    negate 

    negaterow 

    """ 

    return add(matlist1, negate(matlist2, K), K) 

 

 

def negate(matlist, K): 

    """ 

    Negates the elements of a matrix row-wise. 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densearith import negate 

    >>> from sympy import ZZ 

    >>> a = [ 

    ... [ZZ(2), ZZ(3)], 

    ... [ZZ(4), ZZ(5)]] 

    >>> b = [ 

    ... [ZZ(0), ZZ(0)], 

    ... [ZZ(0), ZZ(0)]] 

    >>> negate(a, ZZ) 

    [[-2, -3], [-4, -5]] 

    >>> negate(b, ZZ) 

    [[0, 0], [0, 0]] 

 

    See Also 

    ======== 

 

    negaterow 

    """ 

    return [negaterow(row, K) for row in matlist] 

 

 

def negaterow(row, K): 

    """ 

    Negates a row element-wise. 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densearith import negaterow 

    >>> from sympy import ZZ 

    >>> a = [ZZ(2), ZZ(3), ZZ(4)] 

    >>> b = [ZZ(0), ZZ(0), ZZ(0)] 

    >>> negaterow(a, ZZ) 

    [-2, -3, -4] 

    >>> negaterow(b, ZZ) 

    [0, 0, 0] 

 

    """ 

    return [-element for element in row] 

 

 

def mulmatmat(matlist1, matlist2, K): 

    """ 

    Multiplies two matrices by multiplying each row with each column at 

    a time. The multiplication of row and column is done with mulrowcol. 

 

    Firstly, the second matrix is converted from a list of rows to a 

    list of columns using zip and then multiplication is done. 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densearith import mulmatmat 

    >>> from sympy import ZZ 

    >>> from sympy.matrices.densetools import eye 

    >>> a = [ 

    ... [ZZ(3), ZZ(4)], 

    ... [ZZ(5), ZZ(6)]] 

    >>> b = [ 

    ... [ZZ(1), ZZ(2)], 

    ... [ZZ(7), ZZ(8)]] 

    >>> c = eye(2, ZZ) 

    >>> mulmatmat(a, b, ZZ) 

    [[31, 38], [47, 58]] 

    >>> mulmatmat(a, c, ZZ) 

    [[3, 4], [5, 6]] 

 

    See Also 

    ======== 

 

    mulrowcol 

    """ 

    matcol = [list(i) for i in zip(*matlist2)] 

    result = [] 

    for row in matlist1: 

        result.append([mulrowcol(row, col, K) for col in matcol]) 

    return result 

 

 

def mulmatscaler(matlist, scaler, K): 

    """ 

    Performs scaler matrix multiplication one row at at time. The row-scaler 

    multiplication is done using mulrowscaler. 

 

    Examples 

    ======== 

 

    >>> from sympy import ZZ 

    >>> from sympy.matrices.densearith import mulmatscaler 

    >>> a = [ 

    ... [ZZ(3), ZZ(7), ZZ(4)], 

    ... [ZZ(2), ZZ(4), ZZ(5)], 

    ... [ZZ(6), ZZ(2), ZZ(3)]] 

    >>> mulmatscaler(a, ZZ(1), ZZ) 

    [[3, 7, 4], [2, 4, 5], [6, 2, 3]] 

 

    See Also 

    ======== 

 

    mulscalerrow 

    """ 

    return [mulrowscaler(row, scaler, K) for row in matlist] 

 

 

def mulrowscaler(row, scaler, K): 

    """ 

    Performs the scaler-row multiplication element-wise. 

 

    Examples 

    ======== 

 

    >>> from sympy import ZZ 

    >>> from sympy.matrices.densearith import mulrowscaler 

    >>> a = [ZZ(3), ZZ(4), ZZ(5)] 

    >>> mulrowscaler(a, 2, ZZ) 

    [6, 8, 10] 

 

    """ 

    return [scaler*element for element in row] 

 

 

def mulrowcol(row, col, K): 

    """ 

    Multiplies two lists representing row and column element-wise. 

 

    Gotcha: Here the column is represented as a list contrary to the norm 

    where it is represented as a list of one element lists. The reason is 

    that the theoretically correct approach is too expensive. This problem 

    is expected to be removed later as we have a good data structure to 

    facilitate column operations. 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densearith import mulrowcol 

    >>> from sympy import ZZ 

 

    >>> a = [ZZ(2), ZZ(4), ZZ(6)] 

    >>> mulrowcol(a, a, ZZ) 

    56 

 

    """ 

    result = K.zero 

    for i in range(len(row)): 

        result += row[i]*col[i] 

    return result