forked from fricas/fricas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jet.spad
7399 lines (6142 loc) · 250 KB
/
jet.spad
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Copyright (c) 1993, 1994, Joachim Schue, Werner M. Seiler
-- All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions
-- are met:
--
-- * Redistributions of source code must retain the above
-- copyright notice, this list of conditions and the
-- following disclaimer.
-- * Redistributions in binary form must reproduce the
-- above copyright notice, this list of conditions and
-- the following disclaimer in the documentation and/or
-- other materials provided with the distribution.
-- * Neither the name of the Karlsruhe University nor the
-- names of its contributors may be used to endorse or
-- promote products derived from this software without
-- specific prior written permission.
--
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-- FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-- COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-- POSSIBILITY OF SUCH DAMAGE.
--
-- (C) Version 1 1993 Joachim Schue, Werner M. Seiler
-- (C) Version 2 1994 Werner M. Seiler
--
)abbrev category JBC JetBundleCategory
++ Description:
++ \spadtype{JetBundleCategory} provides basic data structures and
++ procedures for jet bundles. Nearly all necessary functions are implemented
++ already here. Only the representation and functions which directly access
++ it must be implemented in a domain.
++ Two notations of derivatives are supported. Default is multi-index
++ notation, where the i-th entry of the index denotes the number of
++ differentiations taken with respect to \spad{x^i}. In repeated index
++ notation each entry \spad{i} in the index denotes a differentiation
++ with respect to \spad{x^i}. The choice affects, however, only in-
++ and output. Internally, multi-index notation is used throughout.
JetBundleCategory() : Category == Def where
V ==> Vector
B ==> Boolean
SY ==> Symbol
I ==> Integer
PI ==> PositiveInteger
NNI ==> NonNegativeInteger
L ==> List
EI ==> Expression Integer
OUT ==> OutputForm
errmsg1 ==> "Improper multi-index"
errmsg2 ==> "Improper upper index"
errmsg3 ==> "Integration not possible"
Const ==> "Const"::SY
Indep ==> "Indep"::SY
Dep ==> "Dep"::SY
Deriv ==> "Deriv"::SY
Multi ==> "Multi"::SY
Repeated ==> "Repeated"::SY
DerMode ==> [Multi, Repeated]@L(SY)
Def ==> Join(OrderedSet, CoercibleTo EI) with
setNotation : SY -> SY
++ \spad{setNotation(s)} chooses the notation used for derivatives.
++ Returns the old value.
getNotation : () -> SY
++ \spad{getNotation()} shows the currently used notation.
multiIndex : % -> L NNI
++ \spad{multiIndex(jv)} returns the multi-index of the jet
++ variable \spad{jv}.
repeatedIndex : % -> L PI
++ \spad{repeatedIndex(jv)} returns the multi-index of the jet
++ variable \spad{jv} in repeated index notation.
r2m : L PI -> L NNI
++ \spad{r2m(ind)} transforms a repeated index into a multi-index.
m2r : L NNI -> L PI
++ \spad{m2r(ind)} transforms a multi-index into a repeated index.
allRepeated : L NNI -> L L PI
++ \spad{allRepeated(ind)} returns a list of all possible realizations
++ of a given multi-index as repeated index.
index : % -> PI
++ \spad{index(jv)} yields number of the jet variable \spad{jv}.
type : % -> SY
++ \spad{type(jv)} yields the type (\spad{Const, Indep, Dep, Deriv})
++ of the jet variable \spad{jv}.
name : % -> SY
++ \spad{name(jv)} yields the name of the jet variable \spad{jv}.
class : L NNI -> NNI
++ \spad{class(ind)} yields the class of the multi-index \spad{ind}
++ (Position for first non-vanishing entry).
class : % -> NNI
++ \spad{class(jv)} yields the class of the jet variable \spad{jv}
++ (Class of multi-index for derivative, 0 else).
order : % -> NNI
++ \spad{order(jv)} yields the order of the jet variable \spad{jv}
++ (Order as derivative).
weight : % -> NNI
++ \spad{weight(jv)} assigns each jet variable a unique integer
++ reflecting its position in the internal ordering. The variable with
++ the greater weight is also greater in this ordering.
">" : (%, %) -> B
++ \spad{jv1 > jv2} checks whether \spad{jv1} is greater than
++ \spad{jv2} in the internal ordering.
differentiate : (%, PI) -> Union(%, "0")
++ \spad{differentiate(jv, i)} differentiates \spad{jv} wrt the
++ \spad{i}-th independent variable.
derivativeOf? : (%, %) -> L NNI
++ \spad{derivativeOf?(jv1, jv2)} checks whether \spad{jv1} is a
++ derivative of \spad{jv2}. In this case, the difference of their
++ multi-indices is returned. Otherwise, an empty list is returned.
integrateIfCan : (%, PI) -> Union(%, "failed")
++ \spad{integrate(jv, i)} integrated \spad{jv} wrt the \spad{i}-th
++ independent variable, if possible.
integrate : (%, PI) -> %
++ \spad{integrate(jv, i)} is like \spad{integrateIfCan(jv, i)} but
++ yields an error, if the integration is not possible.
X : PI -> %
++ \spad{X(i)} generates the \spad{i}-th independent variable.
U : PI -> %
++ \spad{U(i)} generates the \spad{i}-th dependent variable.
P : (PI, L NNI) -> %
++ \spad{P(i, ind)} generates the derivative of the \spad{i}-th
++ dependent variable wrt the index \spad{ind}. Whether \spad{ind}
++ is interpreted as multi-index or as repeated index depends on the
++ chosen notation.
Pm : (PI, L NNI) -> %
++ \spad{Pm(i, ind)} is like \spad{P(i, ind)} but \spad{ind} is
++ always a multi-index.
Pr : (PI, L PI) -> %
++ \spad{Pr(i, ind)} is like \spad{P(i, ind)} but \spad{ind} is
++ always a repeated index.
1 : constant -> %
++ \spad{1} generates the special "jet variable" 1, which is
++ needed for the representation of linear functions.
one? : % -> B
++ \spad{one?(jv)} checks whether the jet variables \spad{jv}
++ is the special variable 1.
-- For the special cases of only one independent or only one dependent
-- variable simpler calls are provided.
X : () -> %
++ \spad{X()} generates the only independent variable.
U : () -> %
++ \spad{U()} generates the only dependent variable.
P : L NNI -> %
++ \spad{P(ind)} generates the derivative of the only dependent
++ variable wrt the index \spad{ind}.
P : (PI, NNI) -> %
++ \spad{P(i, j)} generates the \spad{j}-th derivative of the
++ \spad{i}-th independent variable wrt the only independent
++ variable.
P : NNI -> %
++ \spad{P(i)} generates the \spad{i}-th derivative of the only
++ dependent variable wrt the only independent variable.
variables : NNI -> L %
++ \spad{variables(q)} computes the list of all jet variables up to
++ order \spad{q}.
variables : (NNI, PI) -> L %
++ \spad{variables(q, c)} computes all jet variables of order \spad{q}
++ whose class is greater than or equal to \spad{c}.
dimJ : NNI -> NNI
++ \spad{dimJ(q)} computes the (fibre) dimension of the \spad{q}-th
++ order jet bundle.
dimS : NNI -> NNI
++ \spad{dimS(q)} computes dimension of SqT x VE
++ (= number of derivatives of order \spad{q}).
numIndVar : () -> PI
++ \spad{numIndVar} returns the number of independent variables.
numDepVar : () -> PI
++ \spad{numDepVar} returns the number of dependent variables.
add
-- Default section.
-- The only procedures not implemented are:
-- multiIndex, index, type, name, X, U, Pm (generic case)
-- coerce, numIndVar, numDepVar, setNotation, getNotation
import from SY
import from List(NNI)
-- global constants for parameters of jet bundle
nn : PI := numIndVar()
mm : PI := numDepVar()
m2r(mi : L NNI) : L PI ==
ri : L PI := []
k : PI := 1
for i in mi repeat
for j in 1..i repeat
ri := cons(k, ri)
k := k+1
ri
r2m(ri : L PI) : L NNI ==
mi : L NNI := new(numIndVar(), 0)
for i in ri repeat
i > nn => error errmsg1
mi.i := 1 + mi.i
mi
allRepeated(mu : L NNI) : L L PI ==
res : L L PI := []
for i in 1..nn for k in mu repeat
if not zero? k then
nu := copy mu
nu.i := (k - 1)::NNI
tmp := allRepeated nu
res := concat!(res, map((x : L PI) : L PI +-> cons(i::PI, x),
tmp))
empty? res => [[]$(L PI)]
res
repeatedIndex(jv : %) : L PI == m2r multiIndex jv
-- ---------------- --
-- Simple Functions --
-- ---------------- --
class(l : L NNI) : NNI ==
res : PI := 1
for i in l while zero? i repeat
res := res + 1
res
class(jv : %) : NNI ==
type(jv) ~= Deriv => 0
class multiIndex jv
order(jv : %) : NNI ==
type(jv) ~= Deriv => 0
sum : NNI := 0
for i in multiIndex jv repeat
sum := sum+i
sum
dimJ(q : NNI) : NNI ==
mm*binomial(q + nn, nn)$Integer ::NNI
dimS(q : NNI) : NNI ==
mm*binomial(q + nn - 1, nn - 1)$Integer ::NNI
X() : % == X(1)
U() : % == U(1)
P(lo : L NNI) : % == P(1, lo)
P(up : PI, lo : NNI) : % == Pm(up, [lo])
P(lo : NNI) : % == Pm(1, [lo])
P(up : PI, lo : L NNI) : % ==
getNotation() = Multi => Pm(up, lo)
lop : L PI := []
for i in lo repeat
zero? i => error errmsg1
lop := cons(i::PI, lop)
Pr(up, reverse! lop)
Pr(up : PI, lo : L PI) : % == Pm(up, r2m lo)
coerce(jv : %) : OUT == name(jv)::OUT
-- ---------- --
-- Dimensions --
-- ---------- --
dimJV : V NNI := new(1, mm)
dimSV : V NNI := new(1, mm)
mn : Integer := minIndex dimJV
-- global vectors with already computed dimensions
dimJ(q : NNI) : NNI ==
q < #dimJV =>
res := qelt(dimJV, mn + q)
res > 0 => res
res := mm*binomial(q + nn, nn)$Integer ::NNI
qsetelt!(dimJV, mn + q, res)
res
oldJV := copy dimJV
dimJV := new(q + 1, 0)
for qq in mn..(mn + #oldJV - 1) repeat
qsetelt!(dimJV, qq, qelt(oldJV, qq))
res := mm*binomial(q + nn, nn)$Integer ::NNI
qsetelt!(dimJV, mn + q, res)
res
dimS(q : NNI) : NNI ==
q < #dimSV =>
res := qelt(dimSV, mn + q)
res > 0 => res
res := mm*binomial(q + nn - 1, nn - 1)$Integer ::NNI
qsetelt!(dimSV, mn + q, res)
res
oldSV := copy dimSV
dimSV := new(q + 1, 0)
for qq in mn..(mn + #oldSV - 1) repeat
qsetelt!(dimSV, qq, qelt(oldSV, qq))
res := mm*binomial(q + nn - 1, nn - 1)$Integer ::NNI
qsetelt!(dimSV, mn + q, res)
res
-- --------------- --
-- Differentiation --
-- --------------- --
differentiate(jv : %, i : PI) : Union(%, "0") ==
i > nn => error errmsg2
jt := type jv
jt = Const => "0"
jt = Indep =>
index(jv) = i => 1
"0"
getNotation() = Multi =>
mind := multiIndex jv
setelt!(mind, i, elt(mind, i - 1 + minIndex(mind)) + 1)
Pm(index jv, mind)
rind := repeatedIndex jv
empty?(rind) => Pr(index jv, [i])
nind : L PI := []
while not empty?(rind) while first(rind) > i repeat
nind := cons(first(rind), nind)
rind := rest rind
nind := concat!(reverse!(nind), cons(i, rind))
Pr(index jv, nind)
derivativeOf?(jv1 : %, jv2 : %) : L NNI ==
type(jv1) ~= Deriv => []
jt := type jv2
jt ~= Deriv and jt ~= Dep => []
index(jv1) ~= index(jv2) => []
res : L NNI := []
for i1 in multiIndex(jv1) for i2 in multiIndex(jv2) repeat
i1 < i2 => return []
res := cons((i1 - i2)::NNI, res)
reverse! res
integrateIfCan(jv : %, i : PI) : Union(%, "failed") ==
i > nn => error errmsg2
type(jv) ~= Deriv => "failed"
getNotation() = Multi =>
mind := multiIndex jv
pos := i - 1 + minIndex mind
mi := qelt(mind, pos)
zero? mi => "failed"
setelt!(mind, pos, (mi - 1)::NNI)
Pm(index jv, mind)
rind := repeatedIndex jv
pos := position(i, rind)
pos < minIndex rind => "failed"
rind := delete(rind, pos)
Pr(index jv, rind)
integrate(jv : %, i : PI) : % ==
ji := integrateIfCan(jv, i)
ji case "failed" => error errmsg3
ji
-- -------- --
-- Ordering --
-- -------- --
weight(jv : %) : NNI ==
t := type jv
t = Const => 0
t = Indep => index jv
t = Dep => (nn + 1)*index(jv)
pos := nn + 1
res := pos*index(jv)
for i in repeatedIndex jv repeat
pos := pos*(nn + 1)
res := res + i*pos
res
one?(jv : %) == type(jv) = Const
jv1 : % = jv2 : % ==
t1 := type jv1
t1 = Const => type(jv2) = Const
t1 = Indep =>
type(jv2) = Indep => index(jv1) = index(jv2)
false
index(jv1) = index(jv2) and multiIndex(jv1) = multiIndex(jv2)
jv1 : % < jv2 : % ==
-- Implements a total degree and class respecting ordering.
-- More efficient than weight(jv1) < weight(jv2).
t1 := type jv1
t2 := type jv2
t2 = Const => false
t1 = Const => true
t1 = Indep =>
t2 = Indep => index(jv1) < index(jv2)
true
t1 = Dep =>
t2 = Indep => false
t2 = Dep => index(jv1) < index(jv2)
true
(t2 = Indep) or (t2 = Dep) => false
o1 := order jv1
o2 := order jv2
o1 = o2 =>
for i1 in multiIndex(jv1) for i2 in multiIndex(jv2) repeat
if i1 ~= i2 then return i1 > i2
index(jv1) < index(jv2)
o1 < o2
jv1 : % > jv2 : % == jv2 < jv1
-- --------- --
-- Variables --
-- --------- --
variables(q : NNI) : L % ==
-- Generates all jet variables up to order q with the exception of 1.
zero? q => [X(i::PI) for i in nn..1 by -1]
OIndList : L L PI := [[i::PI] for i in 1..nn]
IndList : L L PI := [[i::PI] for i in nn..1 by -1]
for qq in 2..q repeat
NIndList : L L PI := []
for ind in OIndList repeat
for j in first(ind)..nn repeat
NIndList := cons(cons(j::PI, ind), NIndList)
OIndList := reverse NIndList
IndList := concat!(NIndList, IndList)
JV : L % := []
for ind in IndList repeat
for k in 1..mm repeat
JV := cons(Pr(k::PI, ind), JV)
concat!(concat!([X(i::PI) for i in 1..nn], _
[U(i::PI) for i in 1..mm]), JV)
variables(q : NNI, c : PI) : L % ==
zero? q => []
OIndList : L L PI := [[i::PI] for i in c..nn]
for qq in 2..q repeat
NIndList : L L PI := []
for ind in OIndList repeat
for j in first(ind)..nn repeat
NIndList := cons(cons(j::PI, ind), NIndList)
OIndList := reverse! NIndList
JV : L % := []
for ind in OIndList repeat
for k in 1..mm repeat
JV := cons(Pr(k::PI, ind), JV)
JV
)abbrev category JBFC JetBundleFunctionCategory
++ Description:
++ \spadtype{JetBundleFunctionCategory} defines the category of functions
++ (local sections) over a jet bundle. The formal derivative is defined
++ already here. It uses the Jacobi matrix of the functions. The columns
++ of the matrices are enumerated by jet variables. Thus they are
++ represented as a \spadtype{Record} of the matrix and a list of the jet
++ variables. Several simplification routines are implemented already here.
JetBundleFunctionCategory(JB : JBC) : Category == Def where
SY ==> Symbol
PI ==> PositiveInteger
NNI ==> NonNegativeInteger
I ==> Integer
B ==> Boolean
L ==> List
OUT ==> OutputForm
JBC ==> JetBundleCategory
SEM ==> SparseEchelonMatrix(JB, %)
SIMPREC ==> Record(Sys : L %, JM : SEM, Depend : Union("failed", L L NNI))
LDREC ==> Record(LD : JB, Fake? : B, Dep : L NNI, Fun : %)
errmsg ==> "cannot simplify"
Def ==> Join(PartialDifferentialRing SY, _
GcdDomain, _
RetractableTo JB) with
-- The following procedures are copied from JetBundleCategory for
-- easier use.
X : PI -> %
U : PI -> %
P : (PI, L NNI) -> %
X : () -> %
U : () -> %
P : L NNI -> %
P : (PI, NNI) -> %
P : NNI -> %
setNotation : SY -> Void
getNotation : () -> SY
numIndVar : () -> PI
numDepVar : () -> PI
coerce : JB -> %
++ \spad{coerce(jv)} coerces the jet variable \spad{jv}
++ into a local section.
jetVariables : % -> L JB
++ \spad{jetVariables(f)} yields all jet variables effectively
++ occurring in \spad{f} in an ordered list.
const? : % -> B
++ \spad{const?(f)} checks whether \spad{f} depends of jet variables.
order : % -> NNI
++ \spad{order(f)} gives highest order of the jet variables
++ effectively occurring in \spad{f}.
class : % -> NNI
++ \spad{class(f)} is defined as the highest class of the
++ jet variables effectively occurring in \spad{f}.
numerator : % -> %
++ \spad{numerator(f)} yields the numerator of \spad{f}.
denominator : % -> %
++ \spad{denominator(f)} yields the denominator of \spad{f}.
jacobiMatrix : L % -> SEM
++ \spad{jacobiMatrix(sys)} constructs the Jacobi matrix
++ of the family \spad{sys} of functions.
jacobiMatrix : (L %, L L JB) -> SEM
++ \spad{jacobiMatrix(sys, jvars)} constructs the Jacobi matrix
++ of the family \spad{sys} of functions. \spad{jvars} contains
++ for each function the effectively occurring jet variables.
++ The columns of the matrix are ordered.
extractSymbol : SEM -> SEM
++ \spad{extractSymbol(jm)} extracts the highest order part of the
++ Jacobi matrix.
symbol : L % -> SEM
++ \spad{symbol(sys)} computes directly the symbol of the family
++ \spad{sys} of functions.
differentiate : (%, JB) -> %
++ \spad{differentiate(f, jv)} differentiates the function
++ \spad{f} wrt the jet variable \spad{jv}.
formalDiff : (%, PI) -> %
++ \spad{formalDiff(f, i)} formally (totally) differentiates
++ \spad{f} wrt the \spad{i}-th independent variable.
formalDiff : (%, L NNI) -> %
++ \spad{formalDiff(f, mu)} formally differentiates \spad{f} as
++ indicated by the multi-index \spad{mu}.
formalDiff : (L %, PI) -> L %
++ \spad{formalDiff(sys, i)} formally differentiates a family
++ \spad{sys} of functions wrt the \spad{i}-th independent
++ variable.
formalDiff2 : (%, PI, SEM) -> Record(DPhi : %, JVars : L JB)
++ \spad{formalDiff2(f, i, jm)} formally differentiates the
++ function \spad{f} with the Jacobi matrix \spad{jm} wrt
++ the \spad{i}-th independent variable. \spad{JVars} is
++ a list of the jet variables effectively in the
++ result \spad{DPhi} (might be too large).
formalDiff2 : (L %, PI, SEM) -> Record(DSys : L %, JVars : L L JB)
++ \spad{formalDiff2(sys, i, jm)} is like the other
++ \spadfun{formalDiff2} but for systems.
dimension : (L %, SEM, NNI) -> NNI
++ \spad{dimension(sys, jm, q)} computes the dimension of the manifold
++ described by the system \spad{sys} with Jacobi matrix \spad{jm}
++ in the jet bundle of order \spad{q}.
orderDim : (L %, SEM, NNI) -> NNI
++ \spad{orderDim(sys, jm, q)} computes the dimension of the manifold
++ described by the system \spad{sys} with Jacobi matrix \spad{jm}
++ in the jet bundle of order \spad{q} over the jet bundle of
++ order \spad{q-1}.
freeOf? : (%, JB) -> B
++ \spad{freeOf?(fun, jv)} checks whether \spad{fun} contains the
++ jet variable \spad{jv}.
subst : (%, JB, %) -> %
++ \spad{subst(f, jv, exp)} substitutes \spad{exp} for the jet
++ variable \spad{jv} in the function \spad{f}.
leadingDer : % -> JB
++ \spad{leadingDer(fun)} yields the leading derivative of \spad{fun}.
++ If \spad{fun} contains no derivatives \spad{1} is returned.
sortLD : L % -> L %
++ \spad{sortLD(sys)} sorts the functions in \spad{sys} according
++ to their leading derivatives.
solveFor : (%, JB) -> Union(%, "failed")
++ \spad{solveFor(fun, jv)} tries to solve \spad{fun} for the jet
++ variable \spad{jv}.
dSubst : (%, JB, %) -> %
++ \spad{dSubst(f, jv, exp)} is like \spad{subst(f, jv, exp)}. But
++ additionally for all derivatives of \spad{jv} the corresponding
++ substitutions are performed.
simplify : (L %, SEM) -> SIMPREC
++ \spad{simplify(sys, jm)} simplifies a system with given Jacobi
++ matrix. The Jacobi matrix of the simplified system is returned, too.
++ \spad{Depend} contains for each equation of the simplified system
++ the numbers of the equations of the original system out of which it
++ is build, if it is possible to obtain this information. If one can
++ generate equations of lower order by purely algebraic operations,
++ then \spad{simplify} should do this.
simpOne : % -> %
++ \spad{simpOne(f)} removes unnecessary coefficients and
++ exponents, denominators etc.
simpMod : (L %, L %) -> L %
++ \spad{simpMod(sys1, sys2)} simplifies the system \spad{sys1}
++ modulo the system \spad{sys2}.
simpMod : (L %, SEM, L %) -> SIMPREC
++ \spad{simpMod(sys1, sys2)} simplifies the system \spad{sys1}
++ modulo the system \spad{sys2}. Returns the same information as
++ \spad{simplify}.
reduceMod : (L %, L %) -> L %
++ \spad{reduceMod(sys1, sys2)} reduces the system \spad{sys1} modulo
++ the system \spad{sys2}.
autoReduce : L % -> L %
++ \spad{autoReduce(sys)} tries to simplify a system by solving each
++ equation for its leading term and substituting it into the other
++ equations.
add
-- Default section.
-- The following functions are already implemented here:
-- const?, order, class, leadingDer, gcd, retractIfCan
-- dSubst, simpOne, simpMod, reduceMod, autoReduce
-- jacobiMatrix, extractSymbol
-- formalDiff, formalDiff2
-- The following procedures must be implemented in the domain
-- coerce
-- basic arithmetics, differentiate
-- jetVariables, subst, solveFor
-- The default version of simplify can treat only simple systems and
-- should be overwritten by a domain specific implementation. The
-- default implementation of gcd always returns 1!
import from List(%)
import from List(JB)
import from List(List(JB))
-- --------- --
-- JBC Stuff --
-- --------- --
nn : PI := numIndVar()$JB
-- global constant
X(i : PI) : % == X(i)$JB ::%
U(i : PI) : % == U(i)$JB ::%
P(i : PI, l : L NNI) : % == P(i, l)$JB ::%
X() : % == X()$JB ::%
U() : % == U()$JB ::%
P(l : L NNI) : % == P(l)$JB ::%
P(i : PI, l : NNI) : % == P(i, l)$JB ::%
P(i : NNI) : % == P(i)$JB ::%
setNotation(s : SY) : Void == setNotation(s)$JB
getNotation() : SY == getNotation()$JB
numIndVar() : PI == numIndVar()$JB
numDepVar() : PI == numDepVar()$JB
-- ---------------- --
-- Simple Functions --
-- ---------------- --
gcd(f1 : %, f2 : %) : % == 1
retractIfCan(f : %) : Union(JB, "failed") ==
JV := jetVariables f
one?(#JV) =>
jv := first JV
one? differentiate(f, jv) => jv
"failed"
"failed"
const?(Phi : %) : B ==
JV := jetVariables Phi
empty?(JV) => true
#JV > 1 => false
first(JV) = 1
order(Phi : %) : NNI == order leadingDer Phi
class(Phi : %) : NNI == class leadingDer Phi
leadingDer(fun : %) : JB ==
JV := jetVariables fun
empty?(JV) => 1
first JV
freeOf?(fun : %, jv : JB) : B == not member?(jv, jetVariables fun)
characteristic() : NNI == 0
dSubst(f : %, jv : JB, exp : %) : % ==
-- Performs for every derivative of jv the corresponding
-- substitution using subst.
of : % := 0
nf : % := f
while nf ~= of repeat
of := nf
JVar : L JB := jetVariables of
for jvar in JVar until jvar < jv repeat
d := derivativeOf?(jvar, jv)
if not empty?(d) then
dexp := formalDiff(exp, d)
nf := subst(nf, jvar, dexp)
nf
-- --------- --
-- Dimension --
-- --------- --
-- The default implementation assumes that sys is simplified and
-- that simplified systems contain only functionally independent
-- equations. There are no checks whether the equations are of
-- correct order in orderDim.
dimension(sys : L %, jm : SEM, q : NNI) : NNI ==
(dimJ(q)$JB - #sys)::NNI
orderDim(sys : L %, jm : SEM, q : NNI) : NNI ==
(dimS(q)$JB - #sys)::NNI
-- --------------- --
-- Jacobi Matrices --
-- --------------- --
noChecks? : B := (% has lazyRepresentation)
-- Global constant. Attribute must be set by domain implementation.
jacobiMatrix(funs : L %) : SEM ==
jacobiMatrix(funs, [jetVariables(fun) for fun in funs])
jacobiMatrix(funs : L %, varlist : L L JB) : SEM ==
-- Computes Jacobi matrix wrt the jet variables in varlist.
-- Each element of varlist contains the variables for one function.
-- It is assumed that these lists are sorted.
-- Returns matrix with sorted columns.
JvList := first varlist
for vars in rest varlist repeat
JvList := removeDuplicates! merge(">", JvList, vars)
JM : SEM := new(JvList, #funs)
for f in funs for vars in varlist for i in 1.. repeat
ents : L % := []
inds : L JB := []
for jv in vars repeat
df := differentiate(f, jv)
if noChecks? or not zero? df then
ents := cons(df, ents)
inds := cons(jv, inds)
setRow!(JM, i, reverse! inds, reverse! ents)
JM
symbol(funs : L %) : SEM ==
JVL : L L JB := [jetVariables fun for fun in funs]
ol : L NNI := [order(first jl)$JB for jl in JVL]
ord := reduce(max, ol, 0)
oJV : L L JB := []
allJV : L JB := []
for jl in JVL repeat
ojl : L JB := []
while not(empty?(jl) or order(first jl)$JB < ord) repeat
ojl := cons(first jl, ojl)
jl := rest jl
ojl := reverse! ojl
oJV := cons(ojl, oJV)
allJV := removeDuplicates! merge(">", allJV, ojl)
oJV := reverse! oJV
symb : SEM := new(allJV, #funs)
for f in funs for ojl in oJV for i in 1.. repeat
ents : L % := []
inds : L JB := []
for jv in ojl repeat
df := differentiate(f, jv)
if noChecks? or not zero? df then
ents := cons(df, ents)
inds := cons(jv, inds)
setRow!(symb, i, reverse! inds, reverse! ents)
symb
extractSymbol(jm : SEM) : SEM ==
inds := allIndices jm
o := order first inds
inds := rest inds
while not(empty?(inds)) and (order(first inds) = o) repeat
inds := rest inds
empty?(inds) => jm
horizSplit(jm, first inds).Left
-- ---------------------- --
-- Formal Differentiation --
-- ---------------------- --
formalDiff(Sys : L %, i : PI) : L % ==
JM := jacobiMatrix Sys
formalDiff2(Sys, i, JM).DSys
formalDiff(Eq : %, i : PI) : % ==
first formalDiff([Eq], i)
formalDiff(f : %, mu : L NNI) : % ==
JV : L JB := jetVariables f
df := f
for i in 1..nn for j in mu repeat
for k in 1..j repeat
jm := jacobiMatrix([df], [JV])
tmp := formalDiff2(df, i::PI, jm)
df := tmp.DPhi
JV := tmp.JVars
df
formalDiff2(Sys : L %, i : PI, JM : SEM
) : Record(DSys : L %, JVars : L L JB) ==
-- Formal differentiation with given Jacobi matrix.
-- Returns list of effectively occurring jet variables for
-- each function.
inds := allIndices JM
empty?(inds) => [[0 for eq in Sys], [[] for eq in Sys]]
LRes : L % := []
LJV : L L JB := []
-- compute formal derivative for each function
for l in 1..nrows(JM) repeat
r := row(JM, l)
res : % := 0
JV : L JB := []
for df in reverse r.Entries for jv in reverse r.Indices repeat
if noChecks? or not zero? df then
djv := differentiate(jv, i)
if djv case "0" then
JV := cons(jv, JV)
else if djv = 1 then
res := res + df
JV := cons(jv, JV)
else
res := res + df*(djv::JB::%)
JV := cons(djv, cons(jv, JV))
LRes := cons(res, LRes)
JV := sort!(">", removeDuplicates! JV)
LJV := cons(JV, LJV)
[reverse! LRes, reverse! LJV]
formalDiff2(Eq : %, i : PI, JM : SEM) : Record(DPhi : %, JVars : L JB) ==
tmp := formalDiff2([Eq], i, JM)
[first tmp.DSys, first tmp.JVars]
-- -------------- --
-- Simplification --
-- -------------- --
-- The simplification routines can be divided into two classes:
-- The first one contains reduceMod and autoReduce. They use dSubst
-- and hence try to reduce the order of the equations. The second
-- class contains simplify, simpOne and simpMod. They do not use dSubst.
-- Thus they can also be applied in a geometric framework, where
-- derivatives are considered as independent variables.
greater(r1 : LDREC, r2 : LDREC) : B == (r1.LD > r2.LD)
-- local function for sorting purposes
sortLD(sys : L %) : L % ==
sl : L LDREC := [[leadingDer(f), false, [1], f] for f in sys]
sl := sort!(greater, sl)
[l.Fun for l in sl]
simpLD(l : L LDREC) : L LDREC ==
-- assumes l is sorted using greater
#l < 2 => l
cur := first l
l := rest l
cur.LD ~= first(l).LD => cons(cur, simpLD l)
eqLD : L LDREC := [cur]
while not(empty?(l)) and (cur.LD = (fl : LDREC := first l).LD) repeat
fl.Fake? => error errmsg
eqLD := cons(fl, eqLD)
l := rest l
-- try to solve one equation and substitute in the other ones
solvable? : B := false
for eq in eqLD until solvable? repeat
s := solveFor(eq.Fun, cur.LD)
solvable? := (s case %)
seq := eq
newL : L LDREC := []
if solvable? then
for eq in eqLD | eq ~= seq repeat
neweq := simpOne subst(eq.Fun, cur.LD, s::%)
if not zero? neweq then
newld := leadingDer neweq
newL := merge(greater, newL, _
[[newld, false, append(eq.Dep, seq.Dep), neweq]$LDREC])
else
-- try to analyse jet variables
seq := first eqLD
sj := jetVariables(seq.Fun)
minlen := #sj
lJV : L L JB := [sj]
for eq in rest eqLD repeat
JV := jetVariables(eq.Fun)
len := #JV
lJV := cons(JV, lJV)
if len < minlen then
seq := eq
sj := JV
minlen := len
lJV := reverse! lJV
if one? minlen then
for eq in eqLD for JV in lJV | eq ~= seq repeat
one?(#JV) => error errmsg
newL := merge(greater, newL, _
[[second(JV), true, eq.Dep, eq.Fun]$LDREC])
else
sjv := second sj
for eq in eqLD for JV in lJV | eq ~= seq repeat
newld := max(second(JV), sjv)
newL := merge(greater, newL,
[[newld, true, eq.Dep, eq.Fun]$LDREC])
cons(seq, simpLD merge(greater, l, newL))
simplify(sys : L %, jm : SEM) : SIMPREC ==
-- Tries to get as far as possible by analysing leading derivatives
-- Will give an error if there are dependent equations in sys
newSys : L % := []
for eq in sys for i in 1.. repeat
neq := simpOne eq
if neq ~= eq then
jmi := jacobiMatrix([neq])
setRow!(jm, i, row(jmi, 1))
newSys := cons(neq, newSys)
newSys := reverse! newSys
sl : L LDREC := [[first(row(jm, i).Indices), false, [i::NNI], f] _
for f in newSys for i in 1..]
sl := simpLD sort!(greater, sl)
resSys := [l.Fun for l in sl]
resDep := [l.Dep for l in sl]