-
Notifications
You must be signed in to change notification settings - Fork 740
/
symengine_tensor_operations.h
1575 lines (1399 loc) · 61.4 KB
/
symengine_tensor_operations.h
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) 2019 - 2020 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE at
// the top level of the deal.II distribution.
//
// ---------------------------------------------------------------------
#ifndef dealii_differentiation_sd_symengine_tensor_operations_h
#define dealii_differentiation_sd_symengine_tensor_operations_h
#include <deal.II/base/config.h>
#ifdef DEAL_II_WITH_SYMENGINE
# include <deal.II/base/symmetric_tensor.h>
# include <deal.II/base/tensor.h>
# include <deal.II/differentiation/sd/symengine_number_types.h>
# include <deal.II/differentiation/sd/symengine_scalar_operations.h>
# include <deal.II/differentiation/sd/symengine_types.h>
# include <utility>
# include <vector>
DEAL_II_NAMESPACE_OPEN
namespace Differentiation
{
namespace SD
{
/**
* @name Symbolic variable creation
*/
//@{
/**
* Return a vector of Expressions representing a vectorial symbolic
* variable with the identifier specified by @p symbol.
*
* For example, if the @p symbol is the string `"v"` then the vectorial
* symbolic variable that is returned represents the vector $v$. Each
* component of $v$ is prefixed by the given @p symbol, and has a suffix that
* indicates its component index.
*
* @tparam dim The dimension of the returned tensor.
* @param[in] symbol An identifier (or name) for the vector of returned
* symbolic variables.
* @return A vector (a rank-1 tensor) of symbolic variables with the name of
* each individual component prefixed by @p symbol.
*
* @warning It is up to the user to ensure that there is no ambiguity in the
* symbols used within a section of code.
*/
template <int dim>
Tensor<1, dim, Expression>
make_vector_of_symbols(const std::string &symbol);
/**
* Return a tensor of Expressions representing a tensorial symbolic
* variable with the identifier specified by @p symbol.
*
* For example, if the @p symbol is the string `"T"` then the tensorial
* symbolic variable that is returned represents the vector $T$. Each
* component of $T$ is prefixed by the given @p symbol, and has a suffix that
* indicates its component indices.
*
* @tparam rank The rank of the returned tensor.
* @tparam dim The dimension of the returned tensor.
* @param[in] symbol An identifier (or name) for the tensor of returned
* symbolic variables.
* @return A tensor of symbolic variables with the name of each individual
* component prefixed by @p symbol.
*
* @warning It is up to the user to ensure that there is no ambiguity in the
* symbols used within a section of code.
*/
template <int rank, int dim>
Tensor<rank, dim, Expression>
make_tensor_of_symbols(const std::string &symbol);
/**
* Return a symmetric tensor of Expressions representing a tensorial
* symbolic variable with the identifier specified by @p symbol.
*
* For example, if the @p symbol is the string `"S"` then the tensorial
* symbolic variable that is returned represents the vector $S$. Each
* component of $S$ is prefixed by the given @p symbol, and has a suffix that
* indicates its component indices.
*
* @tparam rank The rank of the returned tensor.
* @tparam dim The dimension of the returned tensor.
* @param[in] symbol An identifier (or name) for the tensor of returned
* symbolic variables.
* @return A tensor of symbolic variables with the name of each individual
* component prefixed by @p symbol.
*
* @warning It is up to the user to ensure that there is no ambiguity in the
* symbols used within a section of code.
*/
template <int rank, int dim>
SymmetricTensor<rank, dim, Expression>
make_symmetric_tensor_of_symbols(const std::string &symbol);
/**
* Return a vector of Expression representing a vectorial symbolic
* function with the identifier specified by @p symbol. The functions'
* symbolic dependencies are specified by the keys to the input
* @p arguments map; the values stored in the map are ignored.
*
* @tparam dim The dimension of the returned tensor.
* @param[in] symbol An identifier (or name) for the vector of returned
* symbolic functions.
* @param[in] arguments A map of input arguments to the returned
* symbolic functions.
* @return A vector (a rank-1 tensor) of generic symbolic functions with the
* name of each individual component prefixed by @p symbol, a suffix
* that indicates its component index, and the number of input
* arguments equal to the length of @p arguments.
*
* @warning It is up to the user to ensure that there is no ambiguity in the
* symbols used within a section of code.
*/
template <int dim>
Tensor<1, dim, Expression>
make_vector_of_symbolic_functions(const std::string & symbol,
const types::substitution_map &arguments);
/**
* Return a tensor of Expression representing a tensorial symbolic
* function with the identifier specified by @p symbol. The functions'
* symbolic dependencies are specified by the keys to the input
* @p arguments map; the values stored in the map are ignored.
*
* @tparam rank The rank of the returned tensor.
* @tparam dim The dimension of the returned tensor.
* @param[in] symbol An identifier (or name) for the tensor of returned
* symbolic functions.
* @param[in] arguments A map of input arguments to the returned
* symbolic functions.
* @return A tensor of generic symbolic functions with the name of each
* individual component prefixed by @p symbol, a suffix that
* indicates its component indeices, and the number of input
* arguments equal to the length of @p arguments.
*
* @warning It is up to the user to ensure that there is no ambiguity in the
* symbols used within a section of code.
*/
template <int rank, int dim>
Tensor<rank, dim, Expression>
make_tensor_of_symbolic_functions(const std::string & symbol,
const types::substitution_map &arguments);
/**
* Return a symmetric tensor of Expression representing a tensorial
* symbolic function with the identifier specified by @p symbol. The
* functions' symbolic dependencies are specified by the keys to the input
* @p arguments map; the values stored in the map are ignored.
*
* @tparam rank The rank of the returned tensor.
* @tparam dim The dimension of the returned tensor.
* @param[in] symbol An identifier (or name) for the tensor of returned
* symbolic functions.
* @param[in] arguments A map of input arguments to the returned
* symbolic functions.
* @return A symmetric tensor of generic symbolic functions with the name of
* each individual component prefixed by @p symbol, a suffix that
* indicates its component indeices, and the number of input
* arguments equal to the length of @p arguments.
*
* @warning It is up to the user to ensure that there is no ambiguity in the
* symbols used within a section of code.
*/
template <int rank, int dim>
SymmetricTensor<rank, dim, Expression>
make_symmetric_tensor_of_symbolic_functions(
const std::string & symbol,
const types::substitution_map &arguments);
//@}
/**
* @name Symbolic differentiation
*/
//@{
/**
* Return the symbolic result of computing the partial derivative of the
* scalar @p f with respect to the tensor @p T.
*
* @param[in] f A scalar symbolic function or (dependent) expression.
* @param[in] T A tensor of symbolic (independent) variables.
* @return The tensor of symbolic functions or expressions representing
* the result $\frac{\partial f}{\partial \mathbf{T}}$.
*/
template <int rank, int dim>
Tensor<rank, dim, Expression>
differentiate(const Expression &f, const Tensor<rank, dim, Expression> &T);
/**
* Return the symbolic result of computing the partial derivative of the
* scalar @p f with respect to the symmetric tensor @p S.
*
* @param[in] f A scalar symbolic function or (dependent) expression.
* @param[in] S A symmetric tensor of symbolic (independent) variables.
* @return The symmetric tensor of symbolic functions or expressions representing
* the result $\frac{\partial f}{\partial \mathbf{S}}$.
*/
template <int rank, int dim>
SymmetricTensor<rank, dim, Expression>
differentiate(const Expression & f,
const SymmetricTensor<rank, dim, Expression> &S);
/**
* Return the symbolic result of computing the partial derivative of the
* rank-0 tensor (or scalar) @p f with respect to the tensor @p T.
*
* @param[in] f A rank-0 tensor symbolic function or (dependent) expression.
* @param[in] T A tensor of symbolic (independent) variables.
* @return The tensor of symbolic functions or expressions representing
* the result $\frac{\partial f}{\partial \mathbf{T}}$.
*/
template <int rank, int dim>
Tensor<rank, dim, Expression>
differentiate(const Tensor<0, dim, Expression> & f,
const Tensor<rank, dim, Expression> &T);
/**
* Return the symbolic result of computing the partial derivative of the
* rank-0 tensor (or scalar) @p f with respect to the symmetric tensor @p S.
*
* @param[in] f A rank-0 tensor symbolic function or (dependent) expression.
* @param[in] S A symmetric tensor of symbolic (independent) variables.
* @return The symmetric tensor of symbolic functions or expressions representing
* the result $\frac{\partial f}{\partial \mathbf{S}}$.
*/
template <int rank, int dim>
SymmetricTensor<rank, dim, Expression>
differentiate(const Tensor<0, dim, Expression> & f,
const SymmetricTensor<rank, dim, Expression> &S);
/**
* Return the symbolic result of computing the partial derivative of the
* tensor @p T with respect to the scalar @p x.
*
* @param[in] T A tensor of symbolic functions or (dependent) expressions.
* @param[in] x A scalar symbolic (independent) variable.
* @return The tensor of symbolic functions or expressions representing
* the result $\frac{\partial \mathbf{T}}{\partial x}$.
*/
template <int rank, int dim>
Tensor<rank, dim, Expression>
differentiate(const Tensor<rank, dim, Expression> &T, const Expression &x);
/**
* Return the symbolic result of computing the partial derivative of the
* symmetric tensor @p S with respect to the scalar @p x.
*
* @param[in] S A symmetric tensor of symbolic functions or (dependent)
* expressions.
* @param[in] x A scalar symbolic (independent) variable.
* @return The symmetric tensor of symbolic functions or expressions representing
* the result $\frac{\partial \mathbf{S}}{\partial x}$.
*/
template <int rank, int dim>
SymmetricTensor<rank, dim, Expression>
differentiate(const SymmetricTensor<rank, dim, Expression> &S,
const Expression & x);
/**
* Return the symbolic result of computing the partial derivative of the
* tensor @p T with respect to the rank-0 tensor @p x.
*
* @param[in] T A tensor of symbolic functions or (dependent) expressions.
* @param[in] x A rank-0 tensor containing a symbolic (independent)
* variable.
* @return The tensor of symbolic functions or expressions representing
* the result $\frac{\partial \mathbf{T}}{\partial x}$.
*/
template <int rank, int dim>
Tensor<rank, dim, Expression>
differentiate(const Tensor<rank, dim, Expression> &T,
const Tensor<0, dim, Expression> & x);
/**
* Return the symbolic result of computing the partial derivative of the
* symmetric tensor @p S with respect to the rank-0 tensor @p x.
*
* @param[in] S A symmetric tensor of symbolic functions or (dependent)
* expressions.
* @param[in] x A rank-0 tensor containing a symbolic (independent)
* variable.
* @return The symmetric tensor of symbolic functions or expressions representing
* the result $\frac{\partial \mathbf{S}}{\partial x}$.
*/
template <int rank, int dim>
SymmetricTensor<rank, dim, Expression>
differentiate(const SymmetricTensor<rank, dim, Expression> &S,
const Tensor<0, dim, Expression> & x);
/**
* Return the symbolic result of computing the partial derivative of the
* tensor @p T1 with respect to the tensor @p T2.
*
* @param[in] T1 A tensor of symbolic functions or (dependent) expressions.
* @param[in] T2 A tensor of symbolic (independent) variables.
* @return The tensor of symbolic functions or variables representing
* the result $\frac{\partial \mathbf{T}_{1}}{\partial
* \mathbf{T}_{2}}$.
*/
template <int rank_1, int rank_2, int dim>
Tensor<rank_1 + rank_2, dim, Expression>
differentiate(const Tensor<rank_1, dim, Expression> &T1,
const Tensor<rank_2, dim, Expression> &T2);
/**
* Return the symbolic result of computing the partial derivative of the
* symmetric tensor @p S1 with respect to the symmetric tensor @p S2.
*
* @param[in] S1 A symmetric tensor of symbolic functions or (dependent)
* expressions.
* @param[in] S2 A symmetric tensor of symbolic (independent)
* variables.
* @return The symmetric tensor of symbolic functions or variables representing
* the result $\frac{\partial \mathbf{S}_{1}}{\partial
* \mathbf{S}_{2}}$.
*/
template <int rank_1, int rank_2, int dim>
SymmetricTensor<rank_1 + rank_2, dim, Expression>
differentiate(const SymmetricTensor<rank_1, dim, Expression> &S1,
const SymmetricTensor<rank_2, dim, Expression> &S2);
/**
* Return the symbolic result of computing the partial derivative of the
* tensor @p T with respect to the symmetric tensor @p S.
*
* @param[in] T A tensor of symbolic functions or (dependent) expressions.
* @param[in] S A symmetric tensor of symbolic (independent)
* variables.
* @return The tensor of symbolic functions or variables representing
* the result $\frac{\partial \mathbf{T}}{\partial \mathbf{S}}$.
*/
template <int rank_1, int rank_2, int dim>
Tensor<rank_1 + rank_2, dim, Expression>
differentiate(const Tensor<rank_1, dim, Expression> & T,
const SymmetricTensor<rank_2, dim, Expression> &S);
/**
* Return the symbolic result of computing the partial derivative of the
* symmetric tensor @p S with respect to the tensor @p T.
*
* @param[in] S A symmetric tensor of symbolic functions or (dependent)
* expressions.
* @param[in] T A tensor of symbolic (independent) variables.
* @return The tensor of symbolic functions or variables representing
* the result $\frac{\partial \mathbf{S}}{\partial \mathbf{T}}$.
*/
template <int rank_1, int rank_2, int dim>
Tensor<rank_1 + rank_2, dim, Expression>
differentiate(const SymmetricTensor<rank_1, dim, Expression> &S,
const Tensor<rank_2, dim, Expression> & T);
//@}
/**
* @name Symbol map creation and manipulation
*/
//@{
/**
* A convenience function for adding empty entries, with the key values
* equal to the entries in the @p symbol_tensor, to the symbolic
* map @p symbol_map.
*
* For more context which this function is used, see the other
* `add_to_symbol_map(types::substitution_map &, const Expression &)`
* function.
*
* @tparam ignore_invalid_symbols See the other
* `add_to_symbol_map(types::substitution_map &, const Expression &)`
* function for a detailed discussion on the role of this
* template argument.
*
* @tparam SymbolicType A type that represents a symbolic variable.
* The Differentiation::SD::Expression class is often suitable for
* this purpose, although if the @p ValueType is not supported
* by this class then a user-defined @p SymbolicType should be
* used.
* @tparam ValueType A type that corresponds to the @p value that the
* @p symbol is to represent. This @p ValueType is somewhat
* arbitrary as it is only used to create default-constructed
* values as entries in the map.
*/
template <bool ignore_invalid_symbols = false,
typename ValueType = double,
int rank,
int dim,
typename SymbolicType>
void
add_to_symbol_map(types::substitution_map & symbol_map,
const Tensor<rank, dim, SymbolicType> &symbol_tensor);
/**
* A convenience function for adding empty entries, with the key values
* equal to the entries in the @p symbol_tensor, to the symbolic
* map @p symbol_map.
*
* For more context which this function is used, see the other
* `add_to_symbol_map(types::substitution_map &, const Expression &)`
* function.
*
* @tparam ignore_invalid_symbols See the other
* `add_to_symbol_map(types::substitution_map &, const Expression &)`
* function for a detailed discussion on the role of this
* template argument.
*
* @tparam SymbolicType A type that represents a symbolic variable.
* The Differentiation::SD::Expression class is often suitable for
* this purpose, although if the @p ValueType is not supported
* by this class then a user-defined @p SymbolicType should be
* used.
* @tparam ValueType A type that corresponds to the @p value that the
* @p symbol is to represent. This @p ValueType is somewhat
* arbitrary as it is only used to create default-constructed
* values as entries in the map.
*/
template <bool ignore_invalid_symbols = false,
typename ValueType = double,
int rank,
int dim,
typename SymbolicType>
void
add_to_symbol_map(
types::substitution_map & symbol_map,
const SymmetricTensor<rank, dim, SymbolicType> &symbol_tensor);
/**
* Find the input @p symbols in the @p substitution_map and set the entries
* corresponding to the key values given by @p symbol_tensor to the values
* given by @p value_tensor.
*
* This function may be used to safely transform an existing or null
* symbolic map (one with uninitialized entries) into one that can be used
* to conduct symbolic substitution operations (i.e., a substitution map).
*
* @tparam SymbolicType A type that represents a symbolic variable.
* The Differentiation::SD::Expression class is often suitable for
* this purpose, although if the @p ValueType is not supported
* by this class then a user-defined @p SymbolicType should be
* used.
* @tparam ValueType A type that corresponds to the @p value that the
* @p symbol is to represent. Although it is typically
* arithmetic in nature, it may also represent another symbolic
* expression type or be a special type that a user-defined
* @p ExpressionType can be constructed from.
*/
template <int rank, int dim, typename SymbolicType, typename ValueType>
void
set_value_in_symbol_map(
types::substitution_map & substitution_map,
const Tensor<rank, dim, SymbolicType> &symbol_tensor,
const Tensor<rank, dim, ValueType> & value_tensor);
/**
* Find the input @p symbols in the @p substitution_map and set the entries
* corresponding to the key values given by @p symbol_tensor to the values
* given by @p value_tensor.
*
* This function may be used to safely transform an existing or null
* symbolic map (one with uninitialized entries) into one that can be used
* to conduct symbolic substitution operations (i.e., a substitution map).
*
* @tparam SymbolicType A type that represents a symbolic variable.
* The Differentiation::SD::Expression class is often suitable for
* this purpose, although if the @p ValueType is not supported
* by this class then a user-defined @p SymbolicType should be
* used.
* @tparam ValueType A type that corresponds to the @p value that the
* @p symbol is to represent. Although it is typically
* arithmetic in nature, it may also represent another symbolic
* expression type or be a special type that a user-defined
* @p ExpressionType can be constructed from.
*/
template <int rank, int dim, typename SymbolicType, typename ValueType>
void
set_value_in_symbol_map(
types::substitution_map & substitution_map,
const SymmetricTensor<rank, dim, SymbolicType> &symbol_tensor,
const SymmetricTensor<rank, dim, ValueType> & value_tensor);
//@}
/**
* @name Symbol substitution map creation
*/
//@{
/**
* Return a substitution map that has the entry keys given by the
* @p symbol_tensor and the values given by the @p value_tensor. It is
* expected that all key entries be valid symbols or symbolic expressions.
*
* It is possible to map symbolic types to other symbolic types
* using this function. For more details on this, see the other
* `make_substitution_map(const Expression &,const ValueType &)`
* function.
*
* @tparam ExpressionType A type that represents a symbolic expression.
* The Differentiation::SD::Expression class is often suitable for
* this purpose, although if the @p ValueType is not supported
* by this class then a user-defined @p ExpressionType should be
* used.
* @tparam ValueType A type that corresponds to the @p value that the
* @p symbol is to represent. Although it is typically
* arithmetic in nature, it may also represent another symbolic
* expression type or be a special type that a user-defined
* @p ExpressionType can be constructed from.
*/
template <int rank, int dim, typename ExpressionType, typename ValueType>
types::substitution_map
make_substitution_map(
const Tensor<rank, dim, ExpressionType> &symbol_tensor,
const Tensor<rank, dim, ValueType> & value_tensor);
/**
* Return a substitution map that has the entry keys given by the
* @p symbol_tensor and the values given by the @p value_tensor. It is
* expected that all key entries be valid symbols or symbolic expressions.
*
* It is possible to map symbolic types to other symbolic types
* using this function. For more details on this, see the other
* `make_substitution_map(const Expression &,const ValueType &)`
* function.
*
* @tparam ExpressionType A type that represents a symbolic expression.
* The Differentiation::SD::Expression class is often suitable for
* this purpose, although if the @p ValueType is not supported
* by this class then a user-defined @p ExpressionType should be
* used.
* @tparam ValueType A type that corresponds to the @p value that the
* @p symbol is to represent. Although it is typically
* arithmetic in nature, it may also represent another symbolic
* expression type or be a special type that a user-defined
* @p ExpressionType can be constructed from.
*/
template <int rank, int dim, typename ExpressionType, typename ValueType>
types::substitution_map
make_substitution_map(
const SymmetricTensor<rank, dim, ExpressionType> &symbol_tensor,
const SymmetricTensor<rank, dim, ValueType> & value_tensor);
//@}
/**
* @name Symbol substitution map enlargement
*/
//@{
/**
* A convenience function for adding an entry to the @p substitution_map.
* The new entries will have the keys given in the @p symbol_tensor with
* their paired values extracted from the corresponding elements of the
* @p value_tensor.
*
* For more context which this function is used, see the other
* `add_to_substitution_map(types::substitution_map &, const Expression &,
* const Expression &)` function.
*
* @tparam ignore_invalid_symbols See the other
* `add_to_substitution_map(types::substitution_map &, const Expression &,
* const Expression &)` function for a detailed discussion on the role of
* this template argument.
*
* @tparam ExpressionType A type that represents a symbolic expression.
* The Differentiation::SD::Expression class is often suitable for
* this purpose, although if the @p ValueType is not supported
* by this class then a user-defined @p ExpressionType should be
* used.
* @tparam ValueType A type that corresponds to the @p value that the
* @p symbol is to represent. Although it is typically
* arithmetic in nature, it may also represent another symbolic
* expression type or be a special type that a user-defined
* @p ExpressionType can be constructed from.
*/
template <bool ignore_invalid_symbols = false,
int rank,
int dim,
typename ExpressionType,
typename ValueType>
void
add_to_substitution_map(
types::substitution_map & substitution_map,
const Tensor<rank, dim, ExpressionType> &symbol_tensor,
const Tensor<rank, dim, ValueType> & value_tensor);
/**
* A convenience function for adding an entry to the @p substitution_map.
* The new entries will have the keys given in the @p symbol_tensor with
* their paired values extracted from the corresponding elements of the @p value_tensor.
*
* For more context which this function is used, see the other
* `add_to_substitution_map(types::substitution_map &,const Expression &,
* const Expression &)` function.
*
* @tparam ignore_invalid_symbols See the other
* `add_to_substitution_map(types::substitution_map &, const Expression &,
* const Expression &)` function for a detailed discussion on the role of
* this template argument.
*
* @tparam ExpressionType A type that represents a symbolic expression.
* The Differentiation::SD::Expression class is often suitable for
* this purpose, although if the @p ValueType is not supported
* by this class then a user-defined @p ExpressionType should be
* used.
* @tparam ValueType A type that corresponds to the @p value that the
* @p symbol is to represent. Although it is typically
* arithmetic in nature, it may also represent another symbolic
* expression type or be a special type that a user-defined
* @p ExpressionType can be constructed from.
*/
template <bool ignore_invalid_symbols = false,
int rank,
int dim,
typename ExpressionType,
typename ValueType>
void
add_to_substitution_map(
types::substitution_map & substitution_map,
const SymmetricTensor<rank, dim, ExpressionType> &symbol_tensor,
const SymmetricTensor<rank, dim, ValueType> & value_tensor);
//@}
/**
* @name Symbol substitution and evaluation
*/
//@{
/**
* Perform a single substitution sweep of a set of symbols into the given
* tensor of symbolic expressions.
* The symbols in the @p expression_tensor that correspond to the entry keys
* of the @p substitution_map are substituted with the map entry's associated
* value.
* This substitution function may be used to give a set of symbolic
* variables either a numeric interpretation or some symbolic definition.
*
* For more information regarding the performance of symbolic substitution,
* and the outcome of evaluation using a substitution map with cyclic
* dependencies, see the
* `substitute(const Expression &, const types::substitution_map &)`
* function.
*
* @note It is not required that all symbolic expressions be fully resolved
* when using this function. In other words, partial substitutions are
* valid.
*/
template <int rank, int dim>
Tensor<rank, dim, Expression>
substitute(const Tensor<rank, dim, Expression> &expression_tensor,
const types::substitution_map & substitution_map);
/**
* Perform a single substitution sweep of a set of symbols into the given
* symmetric tensor of symbolic expressions.
* The symbols in the @p expression_tensor that correspond to the entry keys
* of the @p substitution_map are substituted with the map entry's associated
* value.
* This substitution function may be used to give a set of symbolic
* variables either a numeric interpretation or some symbolic definition.
*
* For more information regarding the performance of symbolic substitution,
* and the outcome of evaluation using a substitution map with cyclic
* dependencies, see the
* `substitute(const Expression &, const types::substitution_map &)`
* function.
*
* @note It is not required that all symbolic expressions be fully resolved
* when using this function. In other words, partial substitutions are
* valid.
*/
template <int rank, int dim>
SymmetricTensor<rank, dim, Expression>
substitute(const SymmetricTensor<rank, dim, Expression> &expression_tensor,
const types::substitution_map & substitution_map);
/**
* Perform a single substitution sweep of a set of symbols into the given
* tensor of symbolic expressions, and immediately evaluate the tensorial
* result.
* The symbols in the @p expression_tensor that correspond to the entry keys
* of the @p substitution_map are substituted with the map entry's associated
* value.
* This substitution function is used to give a set of symbolic variables
* a numeric interpretation with the returned result being of the type
* specified by the @p ValueType template argument.
*
* For more information regarding the performance of symbolic substitution,
* and the outcome of evaluation using a substitution map with cyclic
* dependencies, see the
* `substitute(const Expression &, const types::substitution_map &)`
* function.
*
* @note It is required that all symbols in @p expression_tensor be
* successfully resolved by the @p substitution_map.
* If only partial substitution is performed, then an error is thrown.
*
* @tparam ValueType A type that corresponds to the @p value that the
* @p symbol is to represent. In the context of this particular
* function, this template parameter is typically arithmetic in
* nature.
*/
template <typename ValueType, int rank, int dim>
Tensor<rank, dim, ValueType>
substitute_and_evaluate(
const Tensor<rank, dim, Expression> &expression_tensor,
const types::substitution_map & substitution_map);
/**
* Perform a single substitution sweep of a set of symbols into the given
* symmetric tensor of symbolic expressions, and immediately evaluate the
* tensorial result.
* The symbols in the @p expression_tensor that correspond to the entry keys
* of the @p substitution_map are substituted with the map entry's associated
* value.
* This substitution function is used to give a set of symbolic variables
* a numeric interpretation, with the returned result being of the type
* specified by the @p ValueType template argument.
*
* For more information regarding the performance of symbolic substitution,
* and the outcome of evaluation using a substitution map with cyclic
* dependencies, see the
* `substitute(const Expression &, const types::substitution_map &)`
* function.
*
* @note It is required that all symbols in @p expression_tensor be
* successfully resolved by the @p substitution_map.
* If only partial substitution is performed, then an error is thrown.
*
* @tparam ValueType A type that corresponds to the @p value that the
* @p symbol is to represent. In the context of this particular
* function, this template parameter is typically arithmetic in
* nature.
*/
template <typename ValueType, int rank, int dim>
SymmetricTensor<rank, dim, ValueType>
substitute_and_evaluate(
const SymmetricTensor<rank, dim, Expression> &expression_tensor,
const types::substitution_map & substitution_map);
//@}
} // namespace SD
} // namespace Differentiation
/* -------------------- inline and template functions ------------------ */
# ifndef DOXYGEN
namespace Differentiation
{
namespace SD
{
/* ---------------- Symbolic differentiation --------------*/
namespace internal
{
template <int dim>
TableIndices<4>
make_rank_4_tensor_indices(const unsigned int idx_i,
const unsigned int idx_j)
{
const TableIndices<2> indices_i(
SymmetricTensor<2, dim>::unrolled_to_component_indices(idx_i));
const TableIndices<2> indices_j(
SymmetricTensor<2, dim>::unrolled_to_component_indices(idx_j));
return TableIndices<4>(indices_i[0],
indices_i[1],
indices_j[0],
indices_j[1]);
}
template <int rank_1, int rank_2>
TableIndices<rank_1 + rank_2>
concatenate_indices(const TableIndices<rank_1> &indices_1,
const TableIndices<rank_2> &indices_2)
{
TableIndices<rank_1 + rank_2> indices_out;
for (unsigned int i = 0; i < rank_1; ++i)
indices_out[i] = indices_1[i];
for (unsigned int j = 0; j < rank_2; ++j)
indices_out[rank_1 + j] = indices_2[j];
return indices_out;
}
template <int rank>
TableIndices<rank>
transpose_indices(const TableIndices<rank> &indices)
{
return indices;
}
template <>
inline TableIndices<2>
transpose_indices(const TableIndices<2> &indices)
{
return TableIndices<2>(indices[1], indices[0]);
}
template <int rank, int dim, typename ValueType>
bool
is_symmetric_component(const TableIndices<rank> &,
const Tensor<rank, dim, ValueType> &)
{
return false;
}
template <int rank, int dim, typename ValueType>
bool
is_symmetric_component(const TableIndices<rank> &,
const SymmetricTensor<rank, dim, ValueType> &)
{
static_assert(
rank == 0 || rank == 2,
"Querying symmetric component for non rank-2 symmetric tensor index is not allowed.");
return false;
}
template <int dim, typename ValueType>
bool
is_symmetric_component(const TableIndices<2> &table_indices,
const SymmetricTensor<2, dim, ValueType> &)
{
return table_indices[0] != table_indices[1];
}
template <int dim,
typename ValueType = Expression,
template <int, int, typename>
class TensorType>
TensorType<0, dim, ValueType>
scalar_diff_tensor(const ValueType & func,
const TensorType<0, dim, ValueType> &op)
{
return differentiate(func, op);
}
template <int rank,
int dim,
typename ValueType = Expression,
template <int, int, typename>
class TensorType>
TensorType<rank, dim, ValueType>
scalar_diff_tensor(const ValueType & func,
const TensorType<rank, dim, ValueType> &op)
{
TensorType<rank, dim, ValueType> out;
for (unsigned int i = 0; i < out.n_independent_components; ++i)
{
const TableIndices<rank> indices(
out.unrolled_to_component_indices(i));
out[indices] = differentiate(func, op[indices]);
if (is_symmetric_component(indices, op))
out[indices] *= 0.5;
}
return out;
}
// Specialization for rank-0 tensor
template <int rank,
int dim,
typename ValueType = Expression,
template <int, int, typename>
class TensorType>
TensorType<rank, dim, ValueType>
tensor_diff_tensor(const TensorType<0, dim, ValueType> & func,
const TensorType<rank, dim, ValueType> &op)
{
TensorType<rank, dim, ValueType> out;
for (unsigned int i = 0; i < out.n_independent_components; ++i)
{
const TableIndices<rank> indices(
out.unrolled_to_component_indices(i));
out[indices] = differentiate(func, op[indices]);
if (is_symmetric_component(indices, op))
out[indices] *= 0.5;
}
return out;
}
template <int rank,
int dim,
typename ValueType = Expression,
template <int, int, typename>
class TensorType>
TensorType<rank, dim, ValueType>
tensor_diff_scalar(const TensorType<rank, dim, ValueType> &funcs,
const ValueType & op)
{
TensorType<rank, dim, ValueType> out;
for (unsigned int i = 0; i < out.n_independent_components; ++i)
{
const TableIndices<rank> indices(
out.unrolled_to_component_indices(i));
out[indices] = differentiate(funcs[indices], op);
}
return out;
}
// Specialization for rank-0 tensor
template <int rank,
int dim,
typename ValueType = Expression,
template <int, int, typename>
class TensorType>
TensorType<rank, dim, ValueType>
tensor_diff_tensor(const TensorType<rank, dim, ValueType> &funcs,
const TensorType<0, dim, ValueType> & op)
{
TensorType<rank, dim, ValueType> out;
for (unsigned int i = 0; i < out.n_independent_components; ++i)
{
const TableIndices<rank> indices(
out.unrolled_to_component_indices(i));
out[indices] = differentiate(funcs[indices], op);
}
return out;
}
// For either symmetric or normal tensors
template <int rank_1,
int rank_2,
int dim,
typename ValueType = Expression,
template <int, int, typename>
class TensorType>
TensorType<rank_1 + rank_2, dim, ValueType>
tensor_diff_tensor(const TensorType<rank_1, dim, ValueType> &funcs,
const TensorType<rank_2, dim, ValueType> &op)
{
TensorType<rank_1 + rank_2, dim, ValueType> out;
for (unsigned int i = 0; i < funcs.n_independent_components; ++i)
{
const TableIndices<rank_1> indices_i(
funcs.unrolled_to_component_indices(i));
for (unsigned int j = 0; j < op.n_independent_components; ++j)
{
const TableIndices<rank_2> indices_j(
op.unrolled_to_component_indices(j));
const TableIndices<rank_1 + rank_2> indices_out =
concatenate_indices(indices_i, indices_j);
out[indices_out] =
differentiate(funcs[indices_i], op[indices_j]);
if (is_symmetric_component(indices_j, op))
out[indices_out] *= 0.5;
}
}
return out;
}
// For mixed symmetric/standard tensors
// The return type is always a standard tensor, since we cannot be sure
// that any symmetries exist in either the function tensor or the
// differential operator.
template <int rank_1,
int rank_2,
int dim,
typename ValueType = Expression,
template <int, int, typename>
class TensorType_1,
template <int, int, typename>
class TensorType_2>
Tensor<rank_1 + rank_2, dim, ValueType>