Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
3774 views
ubuntu2004
1
latex.matrix_delimiters("[", "]")
2
latex.matrix_column_alignment("c")
3
4
class rowOp(SageObject):
5
def __init__(self,optype,row1, row2, scalar=1):
6
self.row1=str(row1)
7
self.row2 = str(row2)
8
self.optype=optype
9
self.scalar = str(scalar)
10
if self.optype == "elementary":
11
self.string = "R_"+self.row1 + r" \to " + "R_"+self.row1 + r" + " + self.scalar + "R_"+self.row2
12
if self.optype=="diagonal":
13
self.string = "R_"+self.row1 + r" \to " + self.scalar + "R_"+self.row1
14
if self.optype=="permutation":
15
self.string = "R_"+self.row1 + r" \leftrightarrow " + "R_"+self.row2
16
17
def _latex_(self):
18
return self.string
19
20
#Used for constructing things like solution sets of systems of equations
21
class setBuilder(SageObject):
22
def __init__(self, element=None, predicate=None):
23
self.element=element
24
self.predicate=predicate
25
26
def _latex_(self):
27
if self.element==None:
28
return r"\left\{\right\}"
29
string=r"\left\{"+latex(self.element)
30
if self.predicate==None:
31
string+=r"\right\}"
32
else:
33
string+=r"\middle|\,"+"".join([latex(p) for p in self.predicate])+r"\right\}"
34
return string
35
36
#Used to force Sage to use {} around a set -- sometimes Sage is dumb about this with vectors and matrices
37
class bracedSet(SageObject):
38
def __init__(self,list):
39
self.list=list
40
41
def _latex_(self):
42
string=r"\left\{"
43
for i in range(0,len(self.list)-1):
44
string+= latex(self.list[i])+","
45
string+= latex(self.list[-1])+r"\right\}"
46
return string
47
48
#Special case of bracedSet for vectors -- forces them into our standard clumn vectors
49
class vectorSet(bracedSet):
50
def __init__(self,vectors):
51
self.list=[column_matrix(v) for v in vectors]
52
53
#Similar to vectorSet, but for typesetting in prose, e.g. "The vectors v1,v2, and v3..."
54
class vectorList(SageObject):
55
def __init__(self,vectors):
56
self.vecList=[column_matrix(v) for v in vectors]
57
58
def _latex_(self):
59
string=""
60
for i in range(0,len(self.vecList)-1):
61
string+= latex(self.vecList[i])+","
62
string+= r"\text{ and }" + latex(self.vecList[-1])
63
return string
64
65
#Typeset a linear combination without simplifying, as Sage likes to do automatically
66
class linearCombination(SageObject):
67
def __init__(self,coeffs,vecs,parentheses=false):
68
self.coefficients=[]
69
self.vectors=[]
70
self.length=min(len(coeffs),len(vecs))
71
for i in range(0,self.length):
72
self.coefficients.append(coeffs[i])
73
self.vectors.append(vecs[i])
74
self.parentheses=parentheses
75
76
def _latex_(self):
77
string=""
78
for i in range(0,self.length-1):
79
string+= latex(self.coefficients[i])
80
if self.parentheses:
81
string+= r"\left("+latex(self.vectors[i])+r"\right)"
82
else:
83
string+=latex(self.vectors[i])
84
string+="+"
85
string+= latex(self.coefficients[-1])
86
if self.parentheses:
87
string+= r"\left("+latex(self.vectors[-1])+r"\right)"
88
else:
89
string+=latex(self.vectors[-1])
90
return string
91
92
#Generic equation, which could be used with polynomial or matrix equations. Often used with a linearCombination passed as leftside
93
class Equation(SageObject):
94
def __init__(self,leftside,rightside):
95
self.lhs = leftside
96
self.rhs = rightside
97
def _latex_(self):
98
return latex(self.lhs)+"="+latex(self.rhs)
99
100
#Vector equation class
101
class vectorEquation(Equation):
102
def __init__(self,A,vars=None):
103
self.matrix=A
104
#Check if column subdivision exists
105
if not self.matrix.subdivisions()[1]:
106
self.matrix=self.matrix.augment(zero_vector(ZZ, len(self.matrix.columns())), subdivide=true)
107
108
#if vars were not supplied, create them
109
if not vars:
110
vars = vector([var("x_"+str(i+1)) for i in range(0,len(self.matrix.subdivision(0,0).columns()))])
111
112
super().__init__(linearCombination(vars,[column_matrix(c) for c in self.matrix.subdivision(0,0).columns()]), column_matrix(A.column(-1)))
113
114
115
116