-
Notifications
You must be signed in to change notification settings - Fork 48
/
expression_v1.py
1819 lines (1575 loc) · 82.3 KB
/
expression_v1.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
'''Expression parser version 1 and namespace.
The syntax of an expression is as follows:
* **Integers** or **decimal numbers** are denoted in the usual way.
Examples: ``1``, ``1.2``, ``.2``. A number may not start with a zero,
except when followed by a dot: ``0.1`` is valid, but ``01`` is not.
* **Variables** are denoted with a string of alphanumeric characters. The
first character may not be a numeral. Unlike Python variables,
underscores are not allowed, as they have a special meaning. If the
variable is an array with one or more axes, all those axes should be
labeled with a latin character, the index, and appended to the variable
with an underscore. For example an array ``a`` with two axes can be
denoted with ``a_ij``. Optionally, a single numeral may be used to
select an item at the concerning axis. Example: in ``a_i0`` the first
axis of ``a`` is labeled ``i`` and the first element of the second axis
is selected. If the same index occurs twice, the trace is taken along
the concerning axes. Example: the trace of the first and third axes of
``b`` is denoted by ``b_iji``. It is invalid to specify an index more
than twice. The following names cannot be used as variables: ``n``,
``δ``, ``$``. The variable named ``x``, or the value of argument
``default_geometry_name``, has a special meaning, detailed below.
* A term, the **product** of two or more arrays or scalars, is denoted by
space-separated variables, constants or compound expressions. Example:
``a b c`` denotes the product of the scalars ``a``, ``b`` and ``c``. A
term may start with a number, but a number is not allowed in other parts
of the term. Example: ``2 a`` denotes two times ``a``; ``2 2 a`` and ``2
a 2``` are invalid. When two arrays in a term have the same index, this
index is summed. Example: ``a_i b_i`` denotes the inner product of ``a``
and ``b`` and ``A_ij b_j``` a matrix vector product. It is not allowed
to use an index more than twice in a term.
* The operator ``/`` denotes a **fraction**. Example: in ``a b / c d`` ``a
b`` is the numerator and ``c d`` the denominator. Both the numerator and
the denominator may start with a number. Example: ``2 a / 3 b``. The
denominator must be a scalar. Example: ``2 / a_i b_i`` is valid, but ``2
a_i / b_i`` is not.
.. warning::
This syntax is different from the Python syntax. In Python ``a*b /
c*d`` is mathematically equivalent to ``a*b*d/c``.
* The operators ``+`` and ``-`` denote **add** and **subtract**. Both
operators should be surrounded by whitespace, e.g. ``a + b``. Both
operands should have the same shape. Example: ``a_ij + b_i c_j`` is a
valid, provided that the lengths of the axes with the same indices match,
but ``a_ij + b_i`` is invalid. At the beginning of an expression or a
compound ``-`` may be used to negate the following term. Example: in
``-a b + c`` the term ``a b`` is negated before adding ``c``. It is not
allowed to negate other terms: ``a + -b`` is invalid, so is ``a -b``.
* An expression surrounded by parentheses is a **compound expression** and
can be used as single entity in a term. Example: ``(a_i + b_i) c_i``
denotes the inner product of ``a_i + b_i`` with ``c_i``.
* **Exponentiation** is denoted by a ``^``, where the left and right
operands should be a number, variable or compound expression and the
right operand should be a scalar. Example: ``a^2`` denotes the square of
``a``, ``a^-2`` denotes ``a`` to the power ``-2`` and ``a^(1 / 2)`` the
square root of ``a``.
* An **argument** is denoted by a name — following the same rules as a
variable name — prefixed with a question mark. An argument is a scalar
or array with a yet unknown value. Example: ``basis_i ?coeffs_i``
denotes the inner product of a basis with unknown coefficient vector
``?coeffs``. If possible the shape of the argument is deduced from the
expression. In the previous example the shape of ``?coeffs`` is equal to
the shape of ``basis``. If the shape cannot be deduced from the
expression the shape should be defined manually (see :func:`parse`).
Arguments and variables live in separate namespaces: ``?x`` and ``x`` are
different entities.
* An argument may be **substituted** by appending without whitespace
``(arg = value)`` to a variable of compound expression, where ``arg`` is
an argument and ``value`` the substitution. The substitution applies to
the variable of compound expression only. The value may be an
expression. Example: ``2 ?x(x = 3 + y)`` is equivalent to ``2 (3 + y)``
and ``2 ?x(x=y) + 3`` is equivalent to ``2 (y) + 3``. It is possible to
apply multiple substitutions. Example: ``(?x + ?y)(x = 1, y = )2`` is
equivalent to ``1 + 2``.
* The **gradient** of a variable to the default geometry — the default
geometry is variable ``x`` unless overriden by the argument
``default_geometry_name`` — is denoted by an underscore, a comma and an
index. If the variable is an array with more than one axis, the
underscore is omitted. Example: ``a_,i`` denotes the gradient of the
scalar ``a`` to the geometry and ``b_i,j`` the gradient of vector ``b``.
The gradient of a compound expression is denoted by an underscore, a
comma and an index. Example: ``(a_i + b_j)_,k`` denotes the gradient of
``a_i + b_j``. The usual summation rules apply and it is allowed to use
a numeral as index. The **surface gradient** is denoted with a semicolon
instead of a comma, but follows the same rules as the gradient otherwise.
Example: ``a_i;j`` is the sufrace gradient of ``a_i`` to the geometry.
* The **normal** of the default geometry is denoted by ``n_i``, where the
index ``i`` may be replaced with an index of choice.
* A **dirac** is denoted by ``δ`` or ``$`` and takes two indices. The
shape of the dirac is deduced from the expression. Example: let ``A`` be
a square matrix with three rows and columns, then ``δ_ij`` in ``(A_ij - λ
δ_ij) x_j`` has three rows and columns as well.
* An expression surrounded by square brackets or curly braces denotes the
**jump** or **mean**, respectively, of the enclosed expression. Example:
``[ a_i ]`` denotes the jump of ``a_i`` and ``{ a_i + b_i }`` denotes the
mean of ``a_i + b_i``.
* A **function call** is denoted by a name — following the same rules as
for a variable name — optionally followed by ``_`` and indices,
optionally followed by ``:`` and indices, directly followed by the left
parenthesis ``(``, without a space. The arguments to the function are
separated by a comma and at least one space. The function is applied
pointwise to the arguments and summation convection is applied to the
result. Example: assume ``mul(...)`` returns the product of its
arguments, then ``mul(x_i, y_j)`` is equivalent to ``x_i y_j`` and
``mul(x_i, y_i)`` to ``x_i y_i``. Functions and variables share a
namespace: defining a variable with the same name as a function renders
the function inaccessible. Functions of the form ``f_i(...)`` and
``f_ij(...)`` etc. generate one and two axes, respectively. Functions
of the form ``f:i(...)`` and ``f:ij(...)`` etc. consume the axes labelled
``i`` and ``i`` and ``j`` respectively. If all axes are consumed, it is
allowed to omit the axes: ``sum(u)`` is equivalent to ``sum:i(u_i)``.
* A **stack** of two or more arrays along an axis is denoted by a ``<``
followed by comma and space separated arrays followed by ``>`` and an
index. All arguments must have the same shape and must not have an axis labelled with the stack axis.
Example: ``<1, 2>_i`` creates an array with components ``1`` and ``2``.
.. _`Einstein Summation Convection`: https://en.wikipedia.org/wiki/Einstein_notation
'''
import re
import collections
import functools
import itertools
import operator
import types as builtin_types
from typing import Any, Callable, Dict, List, Mapping, Optional, overload, Tuple, Union
from . import function, types, warnings
# Convenience function to create a constant in ExpressionAST (details in
# docstring of `parse` below).
_ = lambda arg: (None, arg)
def _sp(count, singular, plural):
'''format ``count``+ ``singular` or ``plural`` depending on ``count``'''
return '{} {}'.format(count, singular if count == 1 else plural)
class ExpressionSyntaxError(Exception):
pass
class AmbiguousAlignmentError(Exception):
pass
class _IntermediateError(Exception):
'''Intermediate exception, to be catched and converted into an ``ExpressionSyntaxError``.'''
def __init__(self, msg, at=None, count=None):
self.msg = msg
self.at = at
self.count = count
super().__init__(msg)
_Token = collections.namedtuple('_Token', ['type', 'data', 'pos'])
_Token.__doc__ = 'An indivisible part of an expression string.'
_Token.type.__doc__ = 'The type of this token.'
_Token.data.__doc__ = 'Substring of the expression string that belongs to this token.'
_Token.pos.__doc__ = 'The start position of the token in the expression string.'
_Length = collections.namedtuple('_Length', ['pos'])
_Length.__doc__ = 'Yet unknown length, introduced at ``pos`` in the expression string.'
_Length.pos.__doc__ = 'The position where this :class:`_Length` is introduced.'
class _Array:
'''ExpressionAST with shape, indices.
The :class:`_Array` class combines an ExpressionAST with shape and indices
and maintains a list of summed indices in the expression string resulting in
this :class:`_Array`.
Attributes
----------
ast : :class:`tuple`
The ExpressionAST (see :func:`parse`).
indices : :class:`str`
The indices of the array represented by the :attr:`ast`.
shape : :class:`tuple` of :class:`int`\\s or :class:`_Length`\\s
The shape of the array represented by the :attr:`ast`.
summed : :class:`frozenset` of indices (:class:`str`)
A set of indices that are summed in the expression string resulting in
this :class:`_Array`. The indices are not allowed in expressions
involving this :class:`_Array`. For example, index `i` in expression
string ``'a_ij b_i'`` is summed and cannot be used in an expression like
``('a_ij b_i) c_i``.
linked_lengths : :class:`frozenset` of :class:`frozensets` of :class:`_Length`\\s and :class:`int`\\s
A set of sets of :class:`_Length`\\s and :class:`int`\\s. A
:class:`_Length` is introduced if an axis of an :class:`_Array` has an
unknown length, e.g. a dirac has two axes of equal, but unknown length.
All class:`_Length`\\s in a set have the same length. If a set contains
an :class:`int` the class:`_Length`\\s are resolved.
ndim : :class:`int`
The number of dimensions of this :class:`_Array`.
Args
----
ast : :class:`tuple`
See :attr:`_Array.ast`.
indices : :class:`str`
See :attr:`_Array.indices`.
shape : :class:`tuple` of :class:`int`\\s or :class:`_Length`\\s
See :attr:`_Array.shape`.
summed : :class:`frozenset` of indices (:class:`str`)
See :attr:`_Array.summed`.
linked_lengths : :class:`frozenset` of :class:`frozensets` of :class:`_Length`\\s and :class:`int`\\s
See :attr:`_Array.linked_lengths`.
'''
@classmethod
def wrap(cls, ast, indices, shape, linked_lengths=None):
'''Create an :class:`_Array` by wrapping ``ast``.
The ``ast`` should be a constant or variable. Duplicate indices are summed
and numeric indices are replaced by a getitem.
'''
if len(indices) != len(shape):
raise _IntermediateError('Expected {}, got {}.'.format(_sp(len(shape), 'index', 'indices'), len(indices)))
return cls._apply_indices(ast, 0, indices, shape, frozenset(), linked_lengths or frozenset())
@classmethod
def _apply_indices(cls, ast, offset, indices, shape, summed, linked_lengths):
'''Wrap ``ast`` in an :class:`_Array`, thereby summing indices occuring twice and applying numeric indices.
When wrapping a variable or gradient the indices of may appear twice,
indicating summation, or numeric, indicating a getitem. This method wraps
``ast`` and applies summation and getitem if needed.
Args
----
ast : :class:`tuple`
See :attr:`_Array.ast`.
offset : :class:`int`
Start at index ``offset`` when looking for indices occuring twice (in
the entire list of ``indices``, not only those in ``indices[offset:]``)
or numeric indices. The list ``indices[offset:]`` is assumed to be
already processed.
indices : :class:`str`
See :attr:`_Array.indices`.
shape : :class:`tuple` of :class:`int`\\s or :class:`_Length`\\s
See :attr:`_Array.shape`.
``indices``.
summed : :class:`frozenset` of indices (:class:`str`)
See :attr:`_Array.summed`.
linked_lengths : :class:`frozenset` of :class:`frozensets` of :class:`_Length`\\s and :class:`int`\\s
See :attr:`_Array.linked_lengths`.
Returns
-------
wrapped_ast : :class:`_Array foo : bar`
'''
summed = set(summed)
linked_lengths = set(linked_lengths)
i = offset
dims = tuple(range(len(indices)))
while i < len(indices):
index = indices[i]
j = indices.index(index)
if '0' <= index <= '9':
index = int(index)
if isinstance(shape[i], int) and index >= shape[i]:
raise _IntermediateError('Index of dimension {} with length {} out of range.'.format(dims[i], shape[i]))
ast = 'getitem', ast, _(i), _(index)
indices = indices[:i] + indices[i+1:]
shape = shape[:i] + shape[i+1:]
dims = dims[:i] + dims[i+1:]
elif index in summed:
raise _IntermediateError('Index {!r} occurs more than twice.'.format(index))
elif j < i:
linked_lengths = set(cls._update_lengths(linked_lengths, index, shape[j], shape[i]))
ast = 'trace', ast, _(j), _(i)
indices = indices[:j] + indices[j+1:i] + indices[i+1:]
shape = shape[:j] + shape[j+1:i] + shape[i+1:]
dims = dims[:j] + dims[j+1:i] + dims[i+1:]
summed.add(index)
i -= 1
else:
if isinstance(shape[i], _Length) and not any(shape[i] in g for g in linked_lengths):
linked_lengths.add(frozenset([shape[i]]))
i += 1
return cls(ast, indices, shape, summed, linked_lengths)
@classmethod
def stack(cls, arrays, index):
'''Stack ``arrays`` along axis ``index``.
The arrays are stacked in given order. All arrays should have matching
shapes, except for the axis labeled ``index``. If an array does not have
the supplied ``index``, the array is expanded with an axis of length one
before stacking. For example, stacking a scalar and an array with shape
``{i: 2}`` along ``i`` gives an array with shape ``{i: 3}``.
Args
----
arrays : a :class:`~collections.abc.Sequence` of :class:`_Array` objects
The arrays to stack.
index : :class:`str`
The index along which to stack the ``arrays``.
Returns
-------
array : :class:`_Array`
The stacked array.
'''
# TODO: assert is_valid_lhs_indices(index)
if len(arrays) == 0:
raise _IntermediateError('Cannot stack 0 arrays.')
if len(set(frozenset(array.indices) - {index} for array in arrays)) != 1:
raise _IntermediateError(
'Cannot stack arrays with unmatched indices (excluding the stack index {!r}): {}.'
.format(index, ', '.join(array.indices for array in arrays)))
if any(index in array.indices for array in arrays):
warnings.deprecation('Concatenating arrays with `<a_i, b_i>_i` syntax is deprecated.')
indices = index + ''.join(i for i in arrays[0].indices if i != index)
arrays = [(array.append_axis(index, 1) if index not in array.indices else array).transpose(indices) for array in arrays]
if len(arrays) == 1:
return arrays[0]
helper = arrays[0].replace(indices=arrays[0].indices[1:], shape=arrays[0].shape[1:])
for other in arrays[1:]:
other = other.replace(indices=other.indices[1:], shape=other.shape[1:])
shape, linked_lengths = helper._join_shapes(other)
helper = helper.replace(shape=shape, linked_lengths=linked_lengths, summed=helper.summed | other.summed)
# Apply `helper.linked_lengths` to all `arrays`. If the lengths at
# `index` is not known at this point, we won't be able to resolve this
# ever, so raise an exception here.
length = 0
for array in arrays:
shape = array._simplify_shape(helper.linked_lengths)
if isinstance(shape[0], _Length):
raise _IntermediateError('Cannot determine the length of the stack axis, because the length at {} is unknown.'.format(shape[0].pos), at=shape[0].pos)
length += shape[0]
ast = ('concatenate',) + tuple(array.ast for array in arrays)
return helper.replace(ast=ast, indices=indices, shape=(length,)+helper.shape)
@staticmethod
def align(*arrays):
'''Align ``arrays`` to the first array.
Args
----
arrays : :class:`_Array`
The arrays to align.
Returns
-------
aligned_arrays : :class:`tuple` of :class:`_Array` objects
The aligned arrays.
'''
assert len(arrays) > 0
if len(set(frozenset(array.indices) for array in arrays)) != 1:
raise _IntermediateError(
'Cannot align arrays with unmatched indices: {}.'
.format(', '.join(array.indices for array in arrays)))
arrays = [array.transpose(arrays[0].indices) for array in arrays]
helper = arrays[0]
for other in arrays[1:]:
shape, linked_lengths = helper._join_shapes(other)
helper = helper.replace(shape=shape, linked_lengths=linked_lengths, summed=helper.summed | other.summed)
return tuple(array.replace(shape=helper.shape, linked_lengths=helper.linked_lengths, summed=helper.summed) for array in arrays)
def __init__(self, ast, indices, shape, summed, linked_lengths):
assert isinstance(indices, str)
self.ast = tuple(ast)
self.indices = indices
self.shape = tuple(shape)
self.summed = frozenset(summed)
self.linked_lengths = frozenset(linked_lengths)
self.ndim = len(self.indices)
def _join_shapes(self, other):
'''Verify ``self + other`` is valid and return the resulting shape and linked lengths.
Args
----
other : :class:`_Array`
Should have the same (order of) indices as this array.
Returns
-------
shape : :class:`tuple`
The simplified shape of ``self + other``.
linked_lengths : :class:`frozenset` of :class:`frozensets` of :class:`_Length`\\s and :class:`int`\\s
See :attr:`_Array.linked_lengths`. Updated with links resulting from
applying ``self + other``.
'''
assert self.indices == other.indices, 'unaligned'
groups = set(self.linked_lengths | other.linked_lengths)
for index, a, b in zip(self.indices, self.shape, other.shape):
if a == b:
continue
if not isinstance(a, _Length) and not isinstance(b, _Length):
raise _IntermediateError('Shapes at index {!r} differ: {}, {}.'.format(index, a, b))
groups.add(frozenset({a, b}))
linked_lengths = self._join_lengths(other, groups)
return self._simplify_shape(linked_lengths), linked_lengths
def _simplify_shape(self, linked_lengths):
'''Return simplified shape by replacing :class:`_Length`\\s with :class:`int`\\s according to the ``linked_lengths``.'''
shape = []
cache = {k: v for v in linked_lengths for k in v}
for length in self.shape:
if isinstance(length, _Length):
for l in cache[length]:
if not isinstance(l, _Length):
length = l
break
shape.append(length)
return shape
def _join_lengths(*args):
'''Return updated linked lengths resulting from ``self + other``.'''
groups = set()
for arg in args:
groups |= arg.linked_lengths if isinstance(arg, _Array) else arg
cache = {}
for g in groups:
# g = frozenset(itertools.chain.from_iterable(map(linked_lenghts.get, g)))
new_g = set()
for k in g:
new_g |= cache.get(k, frozenset([k]))
new_g = frozenset(new_g)
cache.update((k, new_g) for k in new_g)
linked_lengths = frozenset(cache.values())
# Verify.
for g in linked_lengths:
known = tuple(sorted(set(k for k in g if not isinstance(k, _Length))))
if len(known) > 1:
raise _IntermediateError('Axes have different lengths: {}.'.format(', '.join(map(str, known))))
return linked_lengths
@staticmethod
def _update_lengths(linked_lengths, index, a, b):
'''Add link ``a``, ``b`` to ``linked_lengths``.'''
cache = {l: g for g in linked_lengths for l in g}
if a != b:
if not isinstance(a, _Length) and not isinstance(b, _Length):
raise _IntermediateError('Shapes at index {!r} differ: {}, {}.'.format(index, a, b))
g = cache.get(a, frozenset([a])) | cache.get(b, frozenset([b]))
cache.update((k, g) for k in g)
# Verify.
known = tuple(sorted(set(k for k in g if not isinstance(k, _Length))))
if len(known) > 1:
raise _IntermediateError('Shapes at index {!r} differ: {}.'.format(index, ', '.join(map(str, known))))
elif isinstance(a, _Length):
cache.setdefault(a, frozenset([a]))
return frozenset(cache.values())
def __neg__(self):
'''Return -self.'''
return self.replace(ast=('neg', self.ast))
def _add_sub(self, other, op, name):
'''Return op(self, other).'''
if frozenset(self.indices) != frozenset(other.indices):
raise _IntermediateError('Cannot {} arrays with unmatched indices: {!r}, {!r}.'.format(name, self.indices, other.indices))
other = other.transpose(self.indices)
shape, linked_lengths = self._join_shapes(other)
return _Array((op, self.ast, other.ast), self.indices, shape, self.summed, linked_lengths)
def __add__(self, other):
'''Return self+other.'''
return self._add_sub(other, 'add', 'add')
def __sub__(self, other):
'''Return self-other.'''
return self._add_sub(other, 'sub', 'subtract')
def __mul__(self, other):
'''Return self*other.'''
for a, b in ((self, other), (other, self)):
for index in sorted(frozenset(a.indices) | a.summed):
if index in b.summed:
raise _IntermediateError('Index {!r} occurs more than twice.'.format(index))
common = []
for index, length in zip(self.indices, self.shape):
if index in other.indices:
common.append(index)
else:
other = other.append_axis(index, length)
for index, length in zip(other.indices, other.shape):
if index not in self.indices:
self = self.append_axis(index, length)
indices = self.indices
other = other.transpose(indices)
shape, linked_lengths = self._join_shapes(other)
ast = 'mul', self.ast, other.ast
for index in reversed(common):
i = self.indices.index(index)
ast = 'sum', ast, _(i)
indices = indices[:i] + indices[i+1:]
shape = shape[:i] + shape[i+1:]
return _Array(ast, indices, shape, self.summed | other.summed | frozenset(common), linked_lengths)
def __truediv__(self, other):
'''Return self/value.'''
if other.ndim > 0:
raise _IntermediateError('A denominator must have dimension 0.')
for index in sorted((self.summed | set(self.indices)) & other.summed):
raise _IntermediateError('Index {!r} occurs more than twice.'.format(index))
return _Array(('truediv', self.ast, other.ast), self.indices, self.shape, self.summed | other.summed, self._join_lengths(other))
def __pow__(self, other):
'''Return self**value.'''
if other.ndim > 0:
raise _IntermediateError('An exponent must have dimension 0.')
for index in sorted((self.summed | set(self.indices)) & other.summed):
raise _IntermediateError('Index {!r} occurs more than twice.'.format(index))
return _Array(('pow', self.ast, other.ast), self.indices, self.shape, self.summed | other.summed, self._join_lengths(other))
def grad(self, index, geom, type):
'''Return the gradient w.r.t. ``geom``.'''
assert geom.ndim == 1
assert not isinstance(geom.shape[0], _Length)
assert type in ('grad', 'surfgrad')
ast = type, self.ast, _(geom)
return _Array._apply_indices(ast, self.ndim, self.indices+index, self.shape+geom.shape, self.summed, self.linked_lengths)
def derivative(self, arg):
'Return the derivative to ``arg``.'
return _Array._apply_indices(('derivative', self.ast, arg.ast), self.ndim, self.indices+arg.indices, self.shape+arg.shape, self.summed, self.linked_lengths)
def append_axis(self, index, length):
'''Return an :class:`_Array` with one additional axis.'''
if index in self.indices or index in self.summed:
raise _IntermediateError('Duplicate index: {!r}.'.format(index))
linked_lengths = self.linked_lengths
if isinstance(length, _Length):
for group in linked_lengths:
if length in group:
break
else:
linked_lengths |= frozenset({frozenset({length})})
return _Array(('append_axis', self.ast, _(length)), self.indices+index, self.shape+(length,), self.summed, linked_lengths)
def transpose(self, indices):
'''Return an :class:`_Array` transposed according to ``indices``.'''
if len(indices) != len(set(indices)):
raise _IntermediateError('Cannot transpose from {!r} to {!r}: duplicate indices.'.format(self.indices, indices))
elif set(self.indices) != set(indices):
raise _IntermediateError('Cannot transpose from {!r} to {!r}: indices differ.'.format(self.indices, indices))
if self.indices == indices:
return self
else:
transpose = tuple(map(self.indices.index, indices))
shape = tuple(map(self.shape.__getitem__, transpose))
return _Array(('transpose', self.ast, _(transpose)), indices, shape, self.summed, self.linked_lengths)
def replace(self, **updates):
'''Return a copy of this :class:`_Array` with attributes replaced by ``updates``.'''
kwargs = dict(ast=self.ast, indices=self.indices, shape=self.shape, summed=self.summed, linked_lengths=self.linked_lengths)
kwargs.update(updates)
return _Array(**kwargs)
class _ArrayOmittedIndices:
def __init__(self, ast, shape):
self.ast = tuple(ast)
self.shape = tuple(shape)
self.ndim = len(self.shape)
def __add__(self, other):
if self.shape != other.shape:
raise _IntermediateError('Cannot add arrays with omitted indices because the shapes differ: {}, {}.'.format(self.shape, other.shape))
return _ArrayOmittedIndices(('add', self.ast, other.ast), self.shape)
def __sub__(self, other):
if self.shape != other.shape:
raise _IntermediateError('Cannot subtract arrays with omitted indices because the shapes differ: {}, {}.'.format(self.shape, other.shape))
return _ArrayOmittedIndices(('sub', self.ast, other.ast), self.shape)
def __mul__(self, other):
if self.ndim != 0:
raise _IntermediateError('Arrays with omitted indices cannot be multiplied.')
self_ast = self.ast
for n in other.shape:
self_ast = ('append_axis', self_ast, _(n))
return _ArrayOmittedIndices(('mul', self_ast, other.ast), other.shape)
def __neg__(self):
return _ArrayOmittedIndices(('neg', self.ast), self.shape)
def __truediv__(self, other):
if other.ndim > 0:
raise _IntermediateError('A denominator must have dimension 0.')
return _ArrayOmittedIndices(('truediv', self.ast, other.ast), self.shape)
def __pow__(self, other):
if other.ndim > 0:
raise _IntermediateError('An exponent must have dimension 0.')
return _ArrayOmittedIndices(('pow', self.ast, other.ast), self.shape)
def replace(self, ast=None):
return _ArrayOmittedIndices(self.ast if ast is None else ast, self.shape)
class _ExpressionParser:
'''Expression parser
Args
----
expression : :class:`str`
See argument ``expression`` of :func:`parse`.
variables : :class:`dict` of :class:`str` and :class:`nutils.function.Array` pairs
See argument ``variables`` of :func:`parse`.
arg_shapes : :class:`dict` of :class:`str` and :class:`tuple` or :class:`int`\\s pairs
See argument ``arg_shapes`` of :func:`parse`.
default_geometry_name : class:`str`
See argument ``default_geometry_name`` of :func:`parse`.
fixed_lengths : :class:`dict` of :class:`str` and :class:`int`
See argument ``fixed_lengths`` of :func:`parse`.
'''
eye_symbols = '$', 'δ'
normal_symbols = 'n',
def __init__(self, expression, variables, arg_shapes, default_geometry_name, fixed_lengths):
self.expression = expression
self.variables = variables
self.arg_shapes = dict(arg_shapes)
self.default_geometry_name = default_geometry_name
self.fixed_lengths = fixed_lengths
def highlight(f):
'wrap ``f`` in a function that converts ``_IntermediateError`` objects'
def wrapper(self, *args, **kwargs):
if hasattr(self, '_tokens'):
pos = self._next.pos
else:
pos = 0
try:
return f(self, *args, **kwargs)
except _IntermediateError as e:
if e.at is None:
at = pos
count = self._next.pos - pos if self._next.pos > pos else len(self._next.data)
else:
at = e.at
count = 1 if e.count is None else e.count
raise ExpressionSyntaxError(e.msg + '\n' + self.expression + '\n' + ' '*at + '^'*count) from e
return wrapper
def _consume(self):
'advance to next token'
self._index += 1
if self._index >= len(self._tokens):
raise _IntermediateError('Unexpected end of expression.', at=len(self.expression))
return self._current
def _consume_if_whitespace(self):
'advance to next token if it is a whitespace'
if self._next.type == 'whitespace':
self._consume()
@highlight
def _consume_assert_whitespace(self):
'assert the next token is whitespace, skip it, and advance to next token'
if self._consume().type != 'whitespace':
raise _IntermediateError('Missing whitespace.', at=self._current.pos)
@highlight
def _consume_assert_equal(self, value, msg=None):
'assert the next token is equal to ``value``'
token = self._consume()
if token.type != value:
if msg is None:
msg = 'Expected {!r}.'.format(value)
raise _IntermediateError(msg, at=token.pos)
return token
@property
def _current(self):
'the current token'
return self._tokens[self._index]
@property
def _next(self):
'the next token'
return self._tokens[min(len(self._tokens)-1, self._index+1)]
@property
def _next_non_whitespace(self):
'the next non-whitespace token'
return self._tokens[self._index+2] if self._next.type == 'whitespace' else self._next
def _asarray(self, ast, indices_token, shape, omitted_indices):
indices = indices_token.data if indices_token else ''
if omitted_indices:
assert not indices
return _ArrayOmittedIndices(ast, shape)
if len(indices) != len(shape):
raise _IntermediateError('Expected {}, got {}.'.format(_sp(len(shape), 'index', 'indices'), len(indices)))
linked_lengths = set()
for iaxis, (length, index) in enumerate(zip(shape, indices)):
fixed = self.fixed_lengths.get(index)
if fixed is None:
pass
elif isinstance(length, _Length):
linked_lengths.add(frozenset([length, fixed]))
elif fixed != length:
raise _IntermediateError('Length of index {} is fixed at {} but the expression has length {}.'.format(index, fixed, length), at=indices_token.pos+iaxis, count=1)
return _Array.wrap(ast, indices, shape, linked_lengths)
def _get_variable(self, name):
'get variable by ``name`` or raise an error'
value = self.variables.get(name, None)
if value is None:
raise _IntermediateError('Unknown variable: {!r}.'.format(name))
return value
def _get_geometry(self, name):
'get geometry by ``name`` or raise an error'
geom = self._get_variable(name)
if geom.ndim != 1:
raise _IntermediateError('Invalid geometry: expected 1 dimension, but {!r} has {}.'.format(name, geom.ndim))
return geom
def _get_arg(self, name, indices_token):
'get arg by ``name`` or raise an error'
indices = indices_token.data if indices_token else ''
if name in self.arg_shapes:
shape = self.arg_shapes[name]
if len(shape) != len(indices):
raise _IntermediateError('Argument {!r} previously defined with {} instead of {}.'.format(name, _sp(len(shape), 'axis', 'axes'), len(indices)))
else:
shape = tuple(_Length(indices_token.pos+i) for i, j in enumerate(indices))
self.arg_shapes[name] = shape
return self._asarray(('arg', _(name)) + tuple(map(_, shape)), indices_token, shape, False)
@highlight
def parse_lhs_arg(self, seen_lhs):
'parse lhs arg, e.g. the "x_ij" in "x_kk(x_ij=a_ij)"'
token = self._consume()
if token.type != 'variable':
raise _IntermediateError("Expected an argument, e.g. 'argname'.")
if token.data.startswith('?'):
raise _IntermediateError("The argument name at the left hand side of a substitution must not be prefixed by a '?'.")
name = token.data
if name in seen_lhs:
raise _IntermediateError("Argument {!r} occurs more than once.".format(name))
seen_lhs[name] = token
indices = self._consume() if self._next.type == 'indices' else ''
for i, index in enumerate(indices and indices.data):
if index in indices.data[i+1:]:
raise _IntermediateError('Repeated indices are not allowed on the left hand side.')
elif '0' <= index <= '9':
raise _IntermediateError('Numeric indices are not allowed on the left hand side.')
return self._get_arg(name, indices)
@highlight
def parse_var(self, omitted_indices):
'parse a component of a term, e.g. "1", "a_i", "(2 a_i)", "a_i^2", "abs(x)"'
if self._next.type == '(':
self._consume()
value = self.parse_subexpression_cast(omitted_indices)
self._consume_assert_equal(')')
value = value.replace(ast=('group', value.ast))
elif self._next.type == '[':
self._consume()
value = self.parse_subexpression_cast(omitted_indices)
self._consume_assert_equal(']')
value = value.replace(ast=('jump', value.ast))
if self._next.type == 'geometry':
geometry_name = self._consume().data
else:
geometry_name = self.default_geometry_name
if not omitted_indices and self._next.type == 'indices':
geom = self._get_geometry(geometry_name)
warnings.deprecation('`[f]_i` and `[f]_x_i` are deprecated; use `[f] n({x}_i)` instead'.format(x=geometry_name))
value *= self._asarray(('normal', _(geom)), self._consume(), geom.shape, False)
elif self._next.type == '{':
self._consume()
value = self.parse_subexpression_cast(omitted_indices)
self._consume_assert_equal('}')
value = value.replace(ast=('mean', value.ast))
elif not omitted_indices and self._next.type == '<':
self._consume()
args = self.parse_comma_separated(end='>', parse_item=functools.partial(self.parse_subexpression, omitted_indices=False))
indices = self._consume()
if indices.type != 'indices':
raise _IntermediateError('Expected 1 index.', at=indices.pos, count=len(indices.data))
if len(indices.data) != 1:
raise _IntermediateError('Expected 1 index, got {}.'.format(len(indices.data)), at=indices.pos, count=len(indices.data))
if '0' <= indices.data <= '9':
raise _IntermediateError('Expected a non-numeric index, got {!r}.'.format(indices.data), at=indices.pos, count=len(indices.data))
value = _Array.stack(args, indices.data)
elif not omitted_indices and self._next.type == 'jacobian':
nbounds = len(self._consume().data)-1
geometry_name = self._consume_assert_equal('geometry').data
geom = self._get_geometry(geometry_name)
value = self._asarray(('jacobian', _(geom), _(len(geom)-nbounds)), None, (), False)
elif not omitted_indices and self._next.type == 'old-jacobian':
self._consume()
geometry_name = self._consume_assert_equal('geometry').data
geom = self._get_geometry(geometry_name)
value = self._asarray(('jacobian', _(geom), _(None)), None, (), False)
elif not omitted_indices and self._next.type == 'derivative':
self._consume()
target = self._consume()
assert target.type in ('geometry', 'argument')
indices = self._consume() if self._next.type == 'indices' else None
if target.type == 'geometry':
warnings.deprecation('the gradient syntax `dx_i:u` is deprecated; use `d(u, x_i)` instead')
geom = self._get_geometry(target.data)
elif target.type == 'argument':
assert target.data.startswith('?')
warnings.deprecation('the derivative syntax `d?a:u` is deprecated; use `d(u, ?a)` instead')
arg = self._get_arg(target.data[1:], indices)
func = self.parse_var(False)
if target.type == 'geometry':
return func.grad(indices.data if indices else '', geom, 'grad')
else:
return func.derivative(arg)
elif not omitted_indices and self._next.type == 'eye':
self._consume()
indices = self._consume() if self._next.type == 'indices' else None
length = _Length(self._current.pos)
value = self._asarray(('eye', _(length)), indices, (length, length), False)
elif self._next.type == 'variable':
token = self._consume()
name = token.data
if not omitted_indices and name.startswith('?'):
indices = self._consume() if self._next.type == 'indices' else None
value = self._get_arg(name[1:], indices)
elif name not in self.variables and next(t for t in self._tokens[self._index+1:] if t.type not in ('indices', 'consumes')).type == '(': # assume function
if not omitted_indices and self._next.type == 'indices':
generates_token = self._consume()
generates = generates_token.data
generates_shape = tuple(_Length(pos) for pos, index in enumerate(generates, generates_token.pos) if not '0' <= index <= '9')
else:
generates = ''
generates_shape = ()
consumes = self._consume().data if not omitted_indices and self._next.type == 'consumes' else ''
self._consume_assert_equal('(')
args_omitted_indices = None
if omitted_indices:
args_omitted_indices = self.parse_comma_separated(end=')', parse_item=functools.partial(self.parse_subexpression, omitted_indices=True))
elif not consumes:
try:
index = self._index
args_omitted_indices = self.parse_comma_separated(end=')', parse_item=functools.partial(self.parse_subexpression, omitted_indices=True))
except ExpressionSyntaxError:
self._index = index
if args_omitted_indices:
if not all(arg.shape == args_omitted_indices[0].shape for arg in args_omitted_indices):
raise _IntermediateError('All arguments should have the same shape.')
ast = ('call', _(name), _(len(generates)), _(args_omitted_indices[0].ndim), *(arg.ast for arg in args_omitted_indices))
if omitted_indices:
value = _ArrayOmittedIndices(ast, generates_shape)
else:
value = _Array._apply_indices(ast,
offset=0,
indices=generates,
shape=generates_shape,
summed=frozenset(),
linked_lengths=frozenset())
else:
args = self.parse_comma_separated(end=')', parse_item=functools.partial(self.parse_subexpression, omitted_indices=False))
if consumes:
if not all(set(consumes) <= set(arg.indices) for arg in args):
raise _IntermediateError('All axes to be consumed ({}) must be present in all arguments.'.format(consumes))
args = tuple(arg.transpose(''.join(i for i in arg.indices if i not in consumes)+consumes) for arg in args)
value = _Array._apply_indices(ast=('call', _(name), _(len(generates)), _(len(consumes)), *(arg.ast for arg in args)),
offset=0,
indices=''.join(arg.indices[:arg.ndim-len(consumes)] for arg in args)+generates,
shape=sum((arg.shape[:arg.ndim-len(consumes)] for arg in args), ())+generates_shape,
summed=functools.reduce(operator.or_, (arg.summed for arg in args), frozenset(consumes)),
linked_lengths=functools.reduce(operator.or_, (arg.linked_lengths for arg in args), frozenset()))
elif name in self.normal_symbols:
if self._next.type == 'geometry':
warnings.deprecation('the normal syntax with explicitly geometry `n:x_i` is deprecated; use `n(x_i)` instead')
geometry_name = self._consume().data
else:
geometry_name = self.default_geometry_name
geom = self._get_geometry(geometry_name)
if omitted_indices:
value = _ArrayOmittedIndices(('normal', _(geom)), geom.shape)
else:
indices = self._consume() if self._next.type == 'indices' else None
value = self._asarray(('normal', _(geom)), indices, geom.shape, False)
else:
raw = self._get_variable(name)
if omitted_indices:
value = _ArrayOmittedIndices(_(raw), raw.shape)
else:
indices = self._consume() if self._next.type == 'indices' else None
value = self._asarray(_(raw), indices, raw.shape, False)
else:
raise _IntermediateError('Expected a variable, group or function call.')
if not omitted_indices and self._next.type == 'gradient':
gradient = self._consume()
target = self._consume()
assert target.type in ('geometry', 'argument')
indices = self._consume() if self._next.type == 'indices' else None
if target.type == 'geometry':
assert indices
gradtype = {',': 'grad', ';': 'surfgrad'}[gradient.data]
if target.data:
if gradient.data == ',':
warnings.deprecation('the gradient syntax with explicit geometry `u_,x_i` is deprecated; use `d(u, x_i)` instead')
else:
warnings.deprecation('the surface gradient syntax with explicit geometry `u_;x_i` is deprecated; use `surfgrad(u, x_i)` instead')
geom = self._get_geometry(target.data or self.default_geometry_name)
for i, index in enumerate(indices.data):
value = value.grad(index, geom, gradtype)
elif target.type == 'argument':
assert gradient.data == ','
assert target.data.startswith('?')
warnings.deprecation('the derivative to argument syntax `u_,?a` is deprecated; use `d(u, ?a)` instead')
arg = self._get_arg(target.data[1:], indices)
value = value.derivative(arg)
elif not omitted_indices and self._next.type == 'indices':
raise _IntermediateError("Indices can only be specified for variables, e.g. 'a_ij', not for groups, e.g. '(a+b)_ij'.", at=self._next.pos, count=len(self._next.data))
if not omitted_indices and self._next.type == '(':
self._consume()
subs = self.parse_comma_separated(end=')', parse_item=functools.partial(self.parse_substitution, seen_lhs={}))
if not subs:
raise _IntermediateError("Zero substitutions are not allowed.")
ast = ['substitute', value.ast]
links = [] # type: List[LinkedLengths]
for lhs, rhs in subs:
ast += [lhs.ast, rhs.ast]
links += [rhs.linked_lengths, frozenset(frozenset({l, r}) for l, r in zip(lhs.shape, rhs.shape))]
value = value.replace(ast=tuple(ast), linked_lengths=value._join_lengths(*links))
if self._next.type == '^':
token = self._consume()
if self._next.type == '(':
self._consume()
exponent = self.parse_subexpression(omitted_indices)
self._consume_assert_equal(')')
else:
if self._next.type == '-':
self._consume()
negate = True
else: