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

248

249

250

""" 

Fundamental operations of dense matrices. 

The dense matrix is stored as a list of lists 

 

""" 

 

from sympy.core.compatibility import range 

 

 

def trace(matlist, K): 

    """ 

    Returns the trace of a matrix. 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densetools import trace, eye 

    >>> from sympy import ZZ 

    >>> a = [ 

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

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

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

    >>> b = eye(4, ZZ) 

    >>> trace(a, ZZ) 

    10 

    >>> trace(b, ZZ) 

    4 

 

    """ 

    result = K.zero 

    for i in range(len(matlist)): 

        result += matlist[i][i] 

    return result 

 

 

def transpose(matlist, K): 

    """ 

    Returns the transpose of a matrix 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densetools import transpose 

    >>> from sympy import ZZ 

    >>> a = [ 

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

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

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

    >>> transpose(a, ZZ) 

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

 

    """ 

    return [list(a) for a in (zip(*matlist))] 

 

 

def conjugate(matlist, K): 

    """ 

    Returns the conjugate of a matrix row-wise. 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densetools import conjugate 

    >>> from sympy import ZZ 

    >>> a = [ 

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

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

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

    >>> conjugate(a, ZZ) 

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

 

    See Also 

    ======== 

 

    conjugate_row 

    """ 

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

 

 

def conjugate_row(row, K): 

    """ 

    Returns the conjugate of a row element-wise 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densetools import conjugate_row 

    >>> from sympy import ZZ 

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

    >>> conjugate_row(a, ZZ) 

    [3, 2, 6] 

    """ 

    result = [] 

    for r in row: 

        try: 

            result.append(r.conjugate()) 

        except AttributeError: 

            result.append(r) 

    return result 

 

 

def conjugate_transpose(matlist, K): 

    """ 

    Returns the conjugate-transpose of a matrix 

 

    Examples 

    ======== 

 

    >>> from sympy import ZZ 

    >>> from sympy.matrices.densetools import conjugate_transpose 

    >>> a = [ 

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

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

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

    >>> conjugate_transpose(a, ZZ) 

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

    """ 

    return conjugate(transpose(matlist, K), K) 

 

 

def augment(matlist, column, K): 

    """ 

    Augments a matrix and a column. 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densetools import augment 

    >>> from sympy import ZZ 

    >>> a = [ 

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

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

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

    >>> b = [ 

    ... [ZZ(4)], 

    ... [ZZ(5)], 

    ... [ZZ(6)]] 

    >>> augment(a, b, ZZ) 

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

    """ 

    return [row + element for row, element in zip(matlist, column)] 

 

 

def eye(n, K): 

    """ 

    Returns an identity matrix of size n. 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densetools import eye 

    >>> from sympy import ZZ 

    >>> eye(3, ZZ) 

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

    """ 

    result = [] 

    for i in range(n): 

        result.append([]) 

        for j in range(n): 

            if (i == j): 

                result[i].append(K(1)) 

            else: 

                result[i].append(K.zero) 

    return result 

 

 

def row(matlist, i): 

    """ 

    Returns the ith row of a matrix 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densetools import row 

    >>> from sympy import ZZ 

    >>> a = [ 

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

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

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

    >>> row(a, 2) 

    [6, 2, 3] 

    """ 

    return matlist[i] 

 

 

def col(matlist, i): 

    """ 

    Returns the ith column of a matrix 

    Note: Currently very expensive 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densetools import col 

    >>> from sympy import ZZ 

    >>> a = [ 

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

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

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

    >>> col(a, 1) 

    [[7], [4], [2]] 

    """ 

    matcol = [list(l) for l in zip(*matlist)] 

    return [[l] for l in matcol[i]] 

 

 

def rowswap(matlist, index1, index2, K): 

    """ 

    Returns the matrix with index1 row and index2 row swapped 

    """ 

    matlist[index1], matlist[index2] = matlist[index2], matlist[index1] 

    return matlist 

 

 

def rowmul(matlist, index, k,  K): 

    """ 

    Multiplies index row with k 

    """ 

    for i in range(len(matlist[index])): 

        matlist[index][i] = k*matlist[index][i] 

    return matlist 

 

 

def rowadd(matlist, index1, index2 , k, K): 

    """ 

    Adds the index1 row with index2 row which in turn is multiplied by k 

    """ 

    result = [] 

    for i in range(len(matlist[index1])): 

        matlist[index1][i] = (matlist[index1][i] + k*matlist[index2][i]) 

    return matlist 

 

 

def isHermitian(matlist, K): 

    """ 

    Checks whether matrix is hermitian 

 

    Examples 

    ======== 

 

    >>> from sympy.matrices.densetools import isHermitian 

    >>> from sympy import QQ 

    >>> a = [ 

    ... [QQ(2,1), QQ(-1,1), QQ(-1,1)], 

    ... [QQ(0,1), QQ(4,1), QQ(-1,1)], 

    ... [QQ(0,1), QQ(0,1), QQ(3,1)]] 

    >>> isHermitian(a, QQ) 

    False 

    """ 

    return conjugate_transpose(matlist, K) == matlist