Use %%PRETTY-FNAME in more quadpack error messages
[maxima.git] / share / vector / vector_rebuild.usg
blob4d0dadacee5283314025add45222803a1c45a41f
2 ***********************************************************************
4 Some utilities for working with vectors.
6 Copyright (C)  Nov. 2008  Volker van Nek (van dot nek at arcor dot de)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 MA 02110-1301, USA.
23 ***********************************************************************
25 vector_rebuild.mac provides to do basic vector calculations in a
26 readable form. Choose lists or one column matrices to represent vectors.
28 Loading vector_rebuild.mac sets the option variables listarith, 
29 doallmxops and domxmxops to false. Then addition, scalar multiplication 
30 and non commutative multiplication are not carried out automatically. 
31 These operations  need an additional step which is done by vector_simp:
33 (%i1) load(vector_rebuild)$
34 (%i2) line: [1,2]+t*[3,4];
35 (%o2)                          t [3, 4] + [1, 2]
36 (%i3) %, vector_simp;
37 (%o3)                         [3 t + 1, 4 t + 2]
39 Such expanded expressions can be reconstructed by vector_rebuild:
41 (%i4) vector_rebuild(%,[t]);
42 (%o4)                          t [3, 4] + [1, 2]
44 Vector calculations can be directly reconstructed:
46 (%i5) [1,2]+t*[3,4]+[5,6]+(t/2-1)*[7,8]$
47 (%i6) vector_rebuild(%,[t]);
48                                 13
49 (%o6)                        t [--, 8] + [- 1, 0]
50                                 2
52 While working with column vectors it can be useful to pull out the gcd:
54 (%i7) covect([1/2,1/3,1/4])$
55 (%i8) %, vector_factor;
56                                       [ 6 ]
57                                    1  [   ]
58 (%o8)                              -- [ 4 ]
59                                    12 [   ]
60                                       [ 3 ]
62 The option variable vector_factor_minus controls whether a common 
63 minus sign is also pulled out (true) or not (false=default):
65 (%i9) vector_factor([-2,-4]), vector_factor_minus:true;
66 (%o9)                             - 2 [1, 2]
68 The function extract_equations extracts the system of equations from 
69 a vector equation:
71 (%i10) veq: line = [4,3]+s*[2,1];
72 (%o10)               t [3, 4] + [1, 2] = s [2, 1] + [4, 3]
73 (%i11) sys: extract_equations(veq);
74 (%o11)               [3 t + 1 = 2 s + 4, 4 t + 2 = s + 3]
76 This system can now be solved by e.g. algsys.
78 The calculation of a cross product and the vector length is carried 
79 out immediately:
81 (%i12) [1,0,0]~[0,1,0];
82 (%o12)                             [0, 0, 1]
83 (%i13) (v:covect([1,2]), 1/|v|*v);
84                                     1    [ 1 ]
85 (%o13)                           ------- [   ]
86                                  sqrt(5) [ 2 ]
88 ***********************************************************************
90 vector_rebuild.mac is in a very experimental state of development. 
91 Be warned: It is not compatible with vect.mac. 
93 ***********************************************************************
95 Example: Calculating common points of two planes (e.g. identical planes)
97 (%i1) load(vector_rebuild)$
98 (%i2) v([args]):= covect(args)$
99 (%i3) plane1: v(1,2,3)+p*v(4,5,6)+q*v(7,8,9);
100                              [ 7 ]     [ 4 ]   [ 1 ]
101                              [   ]     [   ]   [   ]
102 (%o3)                      q [ 8 ] + p [ 5 ] + [ 2 ]
103                              [   ]     [   ]   [   ]
104                              [ 9 ]     [ 6 ]   [ 3 ]
105 (%i4) plane2: v(3,2,1)+r*v(6,5,4)+s*v(9,8,7)$
106 (%i5) veq: plane1 = plane2;
107                [ 7 ]     [ 4 ]   [ 1 ]     [ 9 ]     [ 6 ]   [ 3 ]
108                [   ]     [   ]   [   ]     [   ]     [   ]   [   ]
109 (%o5)        q [ 8 ] + p [ 5 ] + [ 2 ] = s [ 8 ] + r [ 5 ] + [ 2 ]
110                [   ]     [   ]   [   ]     [   ]     [   ]   [   ]
111                [ 9 ]     [ 6 ]   [ 3 ]     [ 7 ]     [ 4 ]   [ 1 ]
112 (%i6) map(disp, sys: extract_equations(veq))$
113                          7 q + 4 p + 1 = 9 s + 6 r + 3
115                          8 q + 5 p + 2 = 8 s + 5 r + 2
117                          9 q + 6 p + 3 = 7 s + 4 r + 1
119 (%i7) params: algsys(sys,[p,q,r,s]);
120                                 16 %r2 + 13 %r1 + 16
121 (%o7) [[p = %r1, q = %r2, r = - --------------------, 
122                                          3
123                                                          13 %r2 + 10 %r1 + 10
124                                                      s = --------------------]]
125                                                                   3
126 (%i8) params: %, %r1=u, %r2=v;
127                               16 v + 13 u + 16      13 v + 10 u + 10
128 (%o8)   [[p = u, q = v, r = - ----------------, s = ----------------]]
129                                      3                     3
130 (%i9) subst(params,plane1);
131                              [ 7 ]     [ 4 ]   [ 1 ]
132                              [   ]     [   ]   [   ]
133 (%o9)                      v [ 8 ] + u [ 5 ] + [ 2 ]
134                              [   ]     [   ]   [   ]
135                              [ 9 ]     [ 6 ]   [ 3 ]
136 (%i10) subst(params,plane2);
137                                [ 6 ]                    [ 9 ]   [ 3 ]
138              16 v + 13 u + 16  [   ]   13 v + 10 u + 10 [   ]   [   ]
139 (%o10)    (- ----------------) [ 5 ] + ---------------- [ 8 ] + [ 2 ]
140                     3          [   ]          3         [   ]   [   ]
141                                [ 4 ]                    [ 7 ]   [ 1 ]
142 (%i11) vector_rebuild(%,[u,v]);
143                              [ 7 ]     [ 4 ]   [ 1 ]
144                              [   ]     [   ]   [   ]
145 (%o11)                     v [ 8 ] + u [ 5 ] + [ 2 ]
146                              [   ]     [   ]   [   ]
147                              [ 9 ]     [ 6 ]   [ 3 ]
150 ***********************************************************************
152 Some more examples:
154 (%i1) load(vector_rebuild)$
155 (%i2) n: [1,2,3]$
156 (%i3) a: [2,0,1]$
157 (%i4) plane: x.n = a.n;
158 (%o4)                x . [1, 2, 3] = [2, 0, 1] . [1, 2, 3]
159 (%i5) %, vector_simp;
160 (%o5)                          x . [1, 2, 3] = 5
161 (%i6) %, x=[x1,x2,x3], vector_simp;
162 (%o6)                        3 x3 + 2 x2 + x1 = 5
164 ***********************************************************************
166 (%i1) load(vector_rebuild)$
167 (%i2) v([args]):= covect(args)$
168 (%i3) n: v(1,2,3)$
169 (%i4) en: 1/|n|*n;
170                                          [ 1 ]
171                                    1     [   ]
172 (%o4)                           -------- [ 2 ]
173                                 sqrt(14) [   ]
174                                          [ 3 ]
175 (%i5) a: v(2,0,1)$
176 (%i6) plane: x.n = a.n;
177                                [ 1 ]   [ 2 ]   [ 1 ]
178                                [   ]   [   ]   [   ]
179 (%o6)                      x . [ 2 ] = [ 0 ] . [ 2 ]
180                                [   ]   [   ]   [   ]
181                                [ 3 ]   [ 1 ]   [ 3 ]
182 (%i7) %, vector_simp;
183                                      [ 1 ]
184                                      [   ]
185 (%o7)                            x . [ 2 ] = 5
186                                      [   ]
187                                      [ 3 ]
188 (%i8) %, x=v(x1,x2,x3), vector_simp;
189 (%o8)                        3 x3 + 2 x2 + x1 = 5
190 (%i9) point: v(0,1,2)$
191 (%i10) plane, x=point, vector_simp;
192 (%o10)                               8 = 5
193 (%i11) distance: |(point-a)*en|;
194                                    sqrt(17)
195 (%o11)                             --------
196                                    sqrt(14)
199 (%i12) line: a+t*en;
200                                      [ 1 ]   [ 2 ]
201                                t     [   ]   [   ]
202 (%o12)                      -------- [ 2 ] + [ 0 ]
203                             sqrt(14) [   ]   [   ]
204                                      [ 3 ]   [ 1 ]
205 (%i13) vector_simp(%);
206                                [    t         ]
207                                [ -------- + 2 ]
208                                [ sqrt(14)     ]
209                                [              ]
210                                [     2 t      ]
211 (%o13)                         [   --------   ]
212                                [   sqrt(14)   ]
213                                [              ]
214                                [   3 t        ]
215                                [ -------- + 1 ]
216                                [ sqrt(14)     ]
217 (%i14) vector_rebuild(%,[t]), vector_factor;
218                                      [ 1 ]   [ 2 ]
219                                t     [   ]   [   ]
220 (%o14)                      -------- [ 2 ] + [ 0 ]
221                             sqrt(14) [   ]   [   ]
222                                      [ 3 ]   [ 1 ]
223 (%i15) vector_factor(v(-2,-4));
224                                      [ - 1 ]
225 (%o15)                             2 [     ]
226                                      [ - 2 ]
227 (%i16) vector_factor(v(-2,-4)), vector_factor_minus:true;
228                                        [ 1 ]
229 (%o16)                             - 2 [   ]
230                                        [ 2 ]
232 ***********************************************************************
234 Factoring matrices:
236 (%i1) load(vector_rebuild)$
237 (%i2) M: matrix([1,2],[3,-1]);
238                                   [ 1   2  ]
239 (%o2)                             [        ]
240                                   [ 3  - 1 ]
241 (%i3) M_1: M^^-1, vector_factor;
242                                  1 [ 1   2  ]
243 (%o3)                            - [        ]
244                                  7 [ 3  - 1 ]
245 (%i5) M.M_1, vector_simp;
246                                    [ 1  0 ]
247 (%o5)                              [      ]
248                                    [ 0  1 ]
250 ***********************************************************************