This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
/
differential_form_element.py
1304 lines (985 loc) · 36.8 KB
/
differential_form_element.py
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
r"""
Elements of the algebra of differential forms
AUTHORS:
- Joris Vankerschaver (2010-07-25)
"""
#*****************************************************************************
# Copyright (C) 2010 Joris Vankerschaver <joris.vankerschaver@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# The full text of the GPL is available at:
#
# http://www.gnu.org/licenses/
#*****************************************************************************
from __future__ import print_function
from sage.symbolic.ring import SR
from sage.structure.element import RingElement, AlgebraElement
from sage.rings.integer import Integer
from sage.combinat.permutation import Permutation
import six
def sort_subscript(subscript):
"""
A subscript is a range of integers. This function sorts a subscript
in the sense of arranging it in ascending order. The return values
are the sign of the subscript and the sorted subscript, where the
sign is defined as follows:
#. sign == 0 if two or more entries in the subscript were equal.
#. sign == +1, -1 if a positive (resp. negative) permutation was used to sort the subscript.
INPUT:
- ``subscript`` -- a subscript, i.e. a range of not necessarily
distinct integers
OUTPUT:
- Sign of the permutation used to arrange the subscript, where 0
means that the original subscript had two or more entries that
were the same
- Sorted subscript.
EXAMPLES::
sage: from sage.tensor.differential_form_element import sort_subscript
sage: sort_subscript((1, 3, 2))
(-1, (1, 2, 3))
sage: sort_subscript((1, 3))
(1, (1, 3))
sage: sort_subscript((4, 2, 7, 9, 8))
(1, (2, 4, 7, 8, 9))
"""
if len(subscript) == 0:
return 1, ()
sub_list = sorted(subscript)
offsets = [subscript.index(x)+1 for x in sub_list]
# Check that offsets is a true permutation of 1..n
n = len(offsets)
if sum(offsets) != n*(n+1)//2:
sign = 0
else:
sign = Permutation(offsets).signature()
return sign, tuple(sub_list)
class DifferentialFormFormatter:
r"""
This class contains all the functionality to print a differential form in a
graphically pleasing way. This class is called by the ``_latex_`` and
``_repr_`` methods of the DifferentialForm class.
In a nutshell (see the documentation of ``DifferentialForm`` for more
details), differential forms are represented internally as a dictionary,
where the keys are tuples representing the non-zero components of the
form and the values are the component functions. The methods of this
class create string and latex representations out of the specification
of a subscript and a component function.
EXAMPLES::
sage: from sage.tensor.differential_form_element import DifferentialFormFormatter
sage: x, y, z = var('x, y, z')
sage: U = CoordinatePatch((x, y, z))
sage: D = DifferentialFormFormatter(U)
sage: D.repr((0, 2), sin(x*y))
'sin(x*y)*dx/\\dz'
sage: D.latex((0, 2), sin(x*y))
'\\sin\\left(x y\\right) d x \\wedge d z'
sage: D.latex((1, 2), exp(z))
'e^{z} d y \\wedge d z'
"""
def __init__(self, space):
r"""
Construct a differential form formatter. See
``DifferentialFormFormatter`` for more information.
INPUT:
- space -- CoordinatePatch where the differential forms live.
EXAMPLES::
sage: from sage.tensor.differential_form_element import DifferentialFormFormatter
sage: x, y, z = var('x, y, z')
sage: U = CoordinatePatch((x, y, z))
sage: D = DifferentialFormFormatter(U)
sage: D.repr((0, 2), sin(x*y))
'sin(x*y)*dx/\\dz'
"""
self._space = space
def repr(self, comp, fun):
r"""
String representation of a primitive differential form, i.e. a function
times a wedge product of d's of the coordinate functions.
INPUT:
- ``comp`` -- a subscript of a differential form.
- ``fun`` -- the component function of this form.
EXAMPLES::
sage: from sage.tensor.differential_form_element import DifferentialFormFormatter
sage: x, y, z = var('x, y, z')
sage: U = CoordinatePatch((x, y, z))
sage: D = DifferentialFormFormatter(U)
sage: D.repr((0, 1), z^3)
'z^3*dx/\\dy'
"""
str = "/\\".join( \
[('d%r' % self._space.coordinate(c)) for c in comp])
if fun == 1 and len(comp) > 0:
# We have a non-trivial form whose component function is 1,
# so we just return the formatted form part and ignore the 1.
return str
else:
funstr = fun._repr_()
if not self._is_atomic(funstr):
funstr = '(' + funstr + ')'
if len(str) > 0:
return funstr + "*" + str
else:
return funstr
def latex(self, comp, fun):
r"""
Latex representation of a primitive differential form, i.e. a function
times a wedge product of d's of the coordinate functions.
INPUT:
- ``comp`` -- a subscript of a differential form.
- ``fun`` -- the component function of this form.
EXAMPLES::
sage: from sage.tensor.differential_form_element import DifferentialFormFormatter
sage: x, y, z = var('x, y, z')
sage: U = CoordinatePatch((x, y, z))
sage: D = DifferentialFormFormatter(U)
sage: D.latex((0, 1), z^3)
'z^{3} d x \\wedge d y'
sage: D.latex((), 1)
'1'
sage: D.latex((), z^3)
'z^{3}'
sage: D.latex((0,), 1)
'd x'
"""
from sage.misc.latex import latex
s = " \\wedge ".join( \
[('d %s' % latex(self._space.coordinate(c))) for c in comp])
# Make sure this is a string and not a LatexExpr
s = str(s)
# Add coefficient except if it's 1
if fun == 1:
if s:
return s
else:
return "1"
funstr = fun._latex_()
if not self._is_atomic(funstr):
funstr = '(' + funstr + ')'
if s:
s = " " + s
return funstr + s
def _is_atomic(self, str):
r"""
Helper function to check whether a given string expression
is atomic.
EXAMPLES::
sage: x, y, z = var('x, y, z')
sage: U = CoordinatePatch((x, y, z))
sage: from sage.tensor.differential_form_element import DifferentialFormFormatter
sage: D = DifferentialFormFormatter(U)
sage: D._is_atomic('a + b')
False
sage: D._is_atomic('(a + b)')
True
"""
level = 0
for n, c in enumerate(str):
if c == '(':
level += 1
elif c == ')':
level -= 1
if c == '+' or c == '-':
if level == 0 and n > 0:
return False
return True
class DifferentialForm(AlgebraElement):
r"""
Differential form class.
EXAMPLES:
In order to instantiate differential forms of various degree, we begin
by specifying the CoordinatePatch on which they live, as well as their
parent DifferentialForms algebra.
::
sage: x, y, z = var('x, y, z')
sage: U = CoordinatePatch((x, y, z))
sage: F = DifferentialForms(U)
sage: form1 = DifferentialForm(F, 0, sin(x*y)); form1
sin(x*y)
In the previous example, we created a zero-form from a given function.
To create forms of higher degree, we can use the subscript operator
access the various components::
sage: form2 = DifferentialForm(F, 1); form2
0
sage: form2[0] = 1
sage: form2[1] = exp(cos(x))
sage: form2[2] = 1/ln(y)
sage: form2
1/log(y)*dz + dx + e^cos(x)*dy
We may calculate the exterior derivative of a form, and observe that
applying the exterior derivative twice always yields zero::
sage: dform = form1.diff(); dform
y*cos(x*y)*dx + x*cos(x*y)*dy
sage: dform.diff()
0
As can be seen from the previous example, the exterior derivative
increases the degree of a form by one::
sage: form2.degree()
1
sage: form2.diff().degree()
2
The ``d`` function provides a convenient shorthand for applying the
diff member function. Since d appears in other areas of mathematics
as well, this function is not imported in the global namespace
automatically::
sage: from sage.tensor.differential_form_element import d
sage: form2
1/log(y)*dz + dx + e^cos(x)*dy
sage: d(form2)
-1/(y*log(y)^2)*dy/\dz + -e^cos(x)*sin(x)*dx/\dy
sage: form2.diff()
-1/(y*log(y)^2)*dy/\dz + -e^cos(x)*sin(x)*dx/\dy
sage: d(form1) == form1.diff()
True
The wedge product of two forms can be computed by means of the wedge
member function::
sage: form1 = DifferentialForm(F, 2)
sage: form1[0, 1] = exp(z); form1
e^z*dx/\dy
sage: form2 = DifferentialForm(F, 1)
sage: form2[2] = exp(-z)
sage: form1.wedge(form2)
dx/\dy/\dz
For this member function, there exists again a procedural function
which is completely equivalent::
sage: from sage.tensor.differential_form_element import wedge
sage: form1.wedge(form2)
dx/\dy/\dz
sage: wedge(form1, form2)
dx/\dy/\dz
sage: form1.wedge(form2) == wedge(form1, form2)
True
NOTES:
Differential forms are stored behind the screens as dictionaries,
where the keys are the subscripts of the non-zero components, and
the values are those components.
For example, on a
space with coordinates x, y, z, the form
f = sin(x*y) dx /\\ dy + exp(z) dy /\\ dz
would be represented as the dictionary
{(0, 1): sin(x*y), (1, 2): exp(z)}.
Most differential forms are ''sparse'' in the sense that most of
their components are zero, so that this representation is more
efficient than storing all of the components in a vector.
"""
def __init__(self, parent, degree, fun = None):
r"""
Construct a differential form.
INPUT:
- ``parent`` -- Parent algebra of differential forms.
- ``degree`` -- Degree of the differential form.
- ``fun`` (default: None) -- Initialize this differential form with the given function. If the degree is not zero, this argument is silently ignored.
EXAMPLES::
sage: x, y, z = var('x, y, z')
sage: F = DifferentialForms(); F
Algebra of differential forms in the variables x, y, z
sage: f = DifferentialForm(F, 0, sin(z)); f
sin(z)
"""
from sage.tensor.differential_forms import DifferentialForms
if not isinstance(parent, DifferentialForms):
raise TypeError("Parent not an algebra of differential forms.")
RingElement.__init__(self, parent)
self._degree = degree
self._components = {}
if degree == 0 and fun is not None:
self[[]] = fun
def __getitem__(self, subscript):
r"""
Return a given component of the differential form.
INPUT:
- ``subscript`` -- subscript of the component. Must be an integer
or a list of integers.
EXAMPLES::
sage: x, y, z = var('x, y, z')
sage: F = DifferentialForms(); F
Algebra of differential forms in the variables x, y, z
sage: f = DifferentialForm(F, 0, sin(x*y)); f
sin(x*y)
sage: f[()]
sin(x*y)
sage: df = f.diff(); df
y*cos(x*y)*dx + x*cos(x*y)*dy
sage: df[0]
y*cos(x*y)
sage: df[1]
x*cos(x*y)
sage: df[2]
0
"""
if isinstance(subscript, (Integer, int)):
subscript = (subscript, )
else:
subscript = tuple(subscript)
dim = self.parent().base_space().dim()
if any([s >= dim for s in subscript]):
raise ValueError("Index out of bounds.")
if len(subscript) != self._degree:
raise TypeError("%s is not a subscript of degree %s" %\
(subscript, self._degree))
sign, subscript = sort_subscript(subscript)
if subscript in self._components:
return sign*self._components[subscript]
else:
return 0
def __setitem__(self, subscript, fun):
r"""
Modify a given component of the differential form.
INPUT:
- ``subscript`` -- subscript of the component. Must be an integer
or a list of integers.
EXAMPLES::
sage: F = DifferentialForms(); F
Algebra of differential forms in the variables x, y, z
sage: f = DifferentialForm(F, 2)
sage: f[1, 2] = x; f
x*dy/\dz
"""
if isinstance(subscript, (Integer, int)):
subscript = (subscript, )
else:
subscript = tuple(subscript)
dim = self.parent().base_space().dim()
if any([s >= dim for s in subscript]):
raise ValueError("Index out of bounds.")
if len(subscript) != self._degree:
raise TypeError("%s is not a subscript of degree %s" %\
(subscript, self._degree))
sign, subscript = sort_subscript(subscript)
self._components[subscript] = sign*SR(fun)
def is_zero(self):
r"""
Return True if ``self`` is the zero form.
EXAMPLES::
sage: F = DifferentialForms()
sage: f = DifferentialForm(F, 1); f
0
sage: f.is_zero()
True
sage: f[1] = 1
sage: f.is_zero()
False
sage: f.diff()
0
sage: f.diff().is_zero()
True
"""
self._cleanup()
return len(self._components) == 0
def degree(self):
r"""
Return the degree of self.
EXAMPLES::
sage: F = DifferentialForms(); F
Algebra of differential forms in the variables x, y, z
sage: f = DifferentialForm(F, 2)
sage: f[1, 2] = x; f
x*dy/\dz
sage: f.degree()
2
The exterior differential increases the degree of forms by one::
sage: g = f.diff(); g
dx/\dy/\dz
sage: g.degree()
3
"""
return self._degree
def __eq__(self, other):
r"""
Test whether two differential forms are equal.
EXAMPLES::
sage: F = DifferentialForms(); F
Algebra of differential forms in the variables x, y, z
sage: f = DifferentialForm(F, 2)
sage: f[1,2] = x; f
x*dy/\dz
sage: f == f
True
sage: g = DifferentialForm(F, 3)
sage: g[0, 1, 2] = 1; g
dx/\dy/\dz
sage: f == g
False
sage: f.diff() == g
True
"""
if type(other) is type(self):
if self._degree != other._degree:
return False
else:
# TODO: the following two lines are where most of the
# execution time is spent.
self._cleanup()
other._cleanup()
if len(self._components) != len(other._components):
return False
# We compare the component dictionary of both differential
# forms, keeping in mind that the set of keys is
# lexicographically ordered, so that we can simply iterate
# over both dictionaries in one go and compare (key, value)
# pairs as we go along.
for (key1, val1), (key2, val2) in \
zip(six.iteritems(self._components), \
six.iteritems(other._components)):
if key1 != key2 or str(val1) != str(val2):
return False
return True
else:
return False
def __ne__(self, other):
r"""
Test whether two differential forms are not equal.
EXAMPLES::
sage: F = DifferentialForms(); F
Algebra of differential forms in the variables x, y, z
sage: f = DifferentialForm(F, 2)
sage: f[1,2] = x; f
x*dy/\dz
sage: g = DifferentialForm(F, 3)
sage: g[0, 1, 2] = 1; g
dx/\dy/\dz
sage: f != g
True
"""
return not self == other
def _neg_(self):
r"""
Return the negative of self.
EXAMPLES::
sage: x, y, z = var('x, y, z')
sage: F = DifferentialForms()
sage: f = DifferentialForm(F, 1)
sage: f[0] = y
sage: f[1] = -x
sage: f
y*dx + -x*dy
sage: -f
-y*dx + x*dy
sage: -f == f._neg_()
True
"""
neg = DifferentialForm(self.parent(), self._degree)
for comp in self._components:
neg._components[comp] = -self._components[comp]
return neg
def _add_(self, other):
r"""
Add self and other
EXAMPLES::
sage: x, y, z = var('x, y, z')
sage: F = DifferentialForms()
sage: f = DifferentialForm(F, 1)
sage: g = DifferentialForm(F, 1)
sage: f[0] = exp(x); f
e^x*dx
sage: g[1] = sin(y); g
sin(y)*dy
sage: f + g
e^x*dx + sin(y)*dy
sage: f + g == f._add_(g)
True
Forms must have the same degree to be added::
sage: h = DifferentialForm(F, 2)
sage: h[1, 2] = x; h
x*dy/\dz
sage: f + h
Traceback (most recent call last):
...
TypeError: Cannot add forms of degree 1 and 2
Subtraction is implemented by adding the negative::
sage: x, y, z = var('x, y, z')
sage: F = DifferentialForms()
sage: f = DifferentialForm(F, 1)
sage: g = DifferentialForm(F, 1)
sage: f[0] = exp(x); f
e^x*dx
sage: g[1] = sin(y); g
sin(y)*dy
sage: f - g
e^x*dx + -sin(y)*dy
sage: f - g == f._sub_(g)
True
Forms must have the same degree to be subtracted::
sage: h = DifferentialForm(F, 2)
sage: h[1, 2] = x; h
x*dy/\dz
sage: f - h
Traceback (most recent call last):
...
TypeError: Cannot add forms of degree 1 and 2
"""
if self.is_zero():
return other
if other.is_zero():
return self
if self._degree != other._degree:
raise TypeError("Cannot add forms of degree %s and %s" % \
(self._degree, other._degree))
sumform = DifferentialForm(self.parent(), self._degree)
sumform._components = self._components.copy()
for comp, fun in other._components.items():
sumform[comp] += fun
sumform._cleanup()
return sumform
def _cleanup(self):
r"""
Helper function to clean up self, i.e. to remove any
zero components from the dictionary of components.
EXAMPLES::
sage: F = DifferentialForms()
sage: f = DifferentialForm(F, 1)
sage: f[0] = 0
sage: f[1] = 1
sage: f[2] = 0
sage: f._dump_all()
{(2,): 0, (0,): 0, (1,): 1}
sage: f._cleanup()
sage: f._dump_all()
{(1,): 1}
"""
zeros = []
for comp in self._components:
if self._components[comp].is_zero():
zeros.append(comp)
for comp in zeros:
del self._components[comp]
def _dump_all(self):
r"""
Helper function to dump the internal dictionary of form components.
EXAMPLES::
sage: x, y, z = var('x, y, z')
sage: F = DifferentialForms()
sage: f = DifferentialForm(F, 1)
sage: f[1] = exp(cos(x))
sage: f[2] = sin(ln(y))
sage: f
sin(log(y))*dz + e^cos(x)*dy
sage: f._dump_all()
{(2,): sin(log(y)), (1,): e^cos(x)}
sage: g = DifferentialForm(F, 2)
sage: g[1, 2] = x+y+z
sage: g
(x + y + z)*dy/\dz
sage: g._dump_all()
{(1, 2): x + y + z}
"""
print(self._components)
def diff(self):
r"""
Compute the exterior differential of ``self``.
EXAMPLES::
sage: x, y, z = var('x, y, z')
sage: F = DifferentialForms()
sage: f = DifferentialForm(F, 0, sin(x*y)); f
sin(x*y)
sage: f.diff()
y*cos(x*y)*dx + x*cos(x*y)*dy
sage: g = DifferentialForm(F, 1)
sage: g[0] = y/2
sage: g[1] = -x/2
sage: g
1/2*y*dx + -1/2*x*dy
sage: g.diff()
-1*dx/\dy
sage: h = DifferentialForm(F, 2)
sage: h[0, 1] = exp(z)
sage: h.diff()
e^z*dx/\dy/\dz
The square of the exterior differential operator is
identically zero::
sage: f
sin(x*y)
sage: f.diff()
y*cos(x*y)*dx + x*cos(x*y)*dy
sage: f.diff().diff()
0
sage: g.diff().diff()
0
The exterior differential operator is a derivation of degree one
on the space of differential forms. In this example we import the
operator d() as a short-hand for having to call the diff()
member function.
::
sage: from sage.tensor.differential_form_element import d
sage: d(f)
y*cos(x*y)*dx + x*cos(x*y)*dy
sage: d(f).wedge(g) + f.wedge(d(g))
(-x*y*cos(x*y) - sin(x*y))*dx/\dy
sage: d(f.wedge(g))
(-x*y*cos(x*y) - sin(x*y))*dx/\dy
sage: d(f.wedge(g)) == d(f).wedge(g) + f.wedge(d(g))
True
"""
diff_form = DifferentialForm(self.parent(), self._degree + 1)
for comp in self._components:
fun = self._components[comp]
for n, coord in enumerate(self.parent().base_space().coordinates()):
diff_form[(n, ) + comp] += fun.differentiate(coord)
diff_form._cleanup()
return diff_form
def derivative(self, *args, **kwargs):
r"""
Compute the exterior derivative of ``self``. This is the same as
calling the ``diff`` member function.
EXAMPLES::
sage: x, y = var('x, y')
sage: U = CoordinatePatch((x, y))
sage: F = DifferentialForms(U)
sage: q = DifferentialForm(F, 1)
sage: q[0] = -y/2
sage: q[1] = x/2
sage: q.diff()
dx/\dy
sage: q.derivative()
dx/\dy
Invoking ``diff`` on a differential form has the same effect as
calling this member function::
sage: diff(q)
dx/\dy
sage: diff(q) == q.derivative()
True
When additional arguments are supplied to ``diff``, an error is raised,
since only the exterior derivative has intrinsic meaning while
derivatives with respect to the coordinate variables (in whichever
way) are coordinate dependent, and hence not intrinsic.
::
sage: diff(q, x)
Traceback (most recent call last):
...
ValueError: Differentiation of a form does not take any arguments.
"""
if len(args) > 0 or len(kwargs) > 0:
raise ValueError("Differentiation of a form does not take any arguments.")
return self.diff()
def wedge(self, other):
r"""
Returns the wedge product of ``self`` and other.
EXAMPLES::
sage: x, y, z = var('x, y, z')
sage: F = DifferentialForms()
sage: f = DifferentialForm(F, 1)
sage: f[0] = x^2
sage: f[1] = y
sage: f
x^2*dx + y*dy
sage: g = DifferentialForm(F, 1)
sage: g[2] = z^3
sage: g
z^3*dz
sage: f.wedge(g)
y*z^3*dy/\dz + x^2*z^3*dx/\dz
The wedge product is graded commutative::
sage: f.wedge(g)
y*z^3*dy/\dz + x^2*z^3*dx/\dz
sage: g.wedge(f)
-y*z^3*dy/\dz + -x^2*z^3*dx/\dz
sage: f.wedge(f)
0
When the wedge product of forms belonging to different algebras
is computed, an error is raised::
sage: x, y, p, q = var('x, y, p, q')
sage: F = DifferentialForms(CoordinatePatch((x, y)))
sage: G = DifferentialForms(CoordinatePatch((p, q)))
sage: f = DifferentialForm(F, 0, 1); f
1
sage: g = DifferentialForm(G, 0, x); g
x
sage: f.parent()
Algebra of differential forms in the variables x, y
sage: g.parent()
Algebra of differential forms in the variables p, q
sage: f.wedge(g)
Traceback (most recent call last):
...
TypeError: unsupported operand parents for wedge: 'Algebra of differential forms in the variables x, y' and 'Algebra of differential forms in the variables p, q'
"""
if self.parent() != other.parent():
raise TypeError("unsupported operand parents for wedge: " +\
"\'%s\' and \'%s\'" % (self.parent(), other.parent()))
output = DifferentialForm(self.parent(), self._degree + other._degree)
if self._degree + other._degree > self.parent().ngens():
return output
for lcomp, lfun in self._components.items():
for rcomp, rfun in other._components.items():
output[lcomp + rcomp] += lfun*rfun
output._cleanup()
return output
def _mul_(self, other):
r"""
Multiply self and other. This is identical to the wedge operator.
EXAMPLES::
sage: x, y, z = var('x, y, z')
sage: F = DifferentialForms()
sage: f = F.gen(0); f
dx
sage: g = F.gen(1); g
dy
sage: f*g
dx/\dy
sage: f.wedge(g)
dx/\dy
sage: f*g == f.wedge(g)
True
sage: f*g == f._mul_(g)
True
"""
return self.wedge(other)
def _latex_(self):
r"""
Return a latex representation of self.
EXAMPLES::
sage: x, y, z = var('x, y, z')
sage: F = DifferentialForms()
sage: f = DifferentialForm(F, 1)
sage: f[1] = exp(z); f
e^z*dy
sage: latex(f)
e^{z} d y
sage: g = f.diff(); g
-e^z*dy/\dz
sage: latex(g)
-e^{z} d y \wedge d z
sage: latex(g) == g._latex_()
True
"""
if len(self._components) == 0:
return '0'
format = DifferentialFormFormatter(self.parent().base_space())
output = [format.latex(comp, fun) \
for (comp, fun) in self._components.items()]
return ' + '.join(output)
def _repr_(self):
r"""
Return string representation of self.
EXAMPLES::
sage: x, y, z = var('x, y, z')
sage: F = DifferentialForms()
sage: f = DifferentialForm(F, 1)
sage: f[1] = exp(z); f
e^z*dy