Repository: einsteinpy/einsteinpy

Issue:
Implement Lorentz transform feature for Tensors(Symbolic Module)
🐞 **Problem**
Given a Lorentz matrix, give an option to change coordinates of tensors, taking into account the index configuration of tensors.


💡 **Possible solutions**

![image](https://user-images.githubusercontent.com/24695764/71483412-c0b5ca00-282d-11ea-9963-6e1e7378bc8f.jpg)


📋  **Steps to solve the problem**

 * Comment below about what you've started working on.
 * Add, commit, push your changes
 * Submit a pull request and add this in comments - `Addresses #<put issue number here>`
 * Ask for a review in comments section of pull request
 * Celebrate your contribution to this project 🎉


Discussion:

Patch:
diff --git a/src/einsteinpy/symbolic/__init__.py b/src/einsteinpy/symbolic/__init__.py
index 0ac77d1..ac0ec8f 100644
--- a/src/einsteinpy/symbolic/__init__.py
+++ b/src/einsteinpy/symbolic/__init__.py
@@ -1,3 +1,4 @@
+from .auxillary_functions import simplify_sympy_array
 from .christoffel import ChristoffelSymbols
 from .constants import SymbolicConstant, get_constant
 from .einstein import EinsteinTensor
diff --git a/src/einsteinpy/symbolic/auxillary_functions.py b/src/einsteinpy/symbolic/auxillary_functions.py
new file mode 100644
index 0000000..46fcd32
--- /dev/null
+++ b/src/einsteinpy/symbolic/auxillary_functions.py
@@ -0,0 +1,16 @@
+import sympy
+
+
+def _flatten_list(seq):
+    # flatten an arbitarily nested list
+    if not seq:
+        return []
+    if not isinstance(seq[0], list):
+        return [seq[0]] + _flatten_list(seq[1:])
+    return _flatten_list(seq[0]) + _flatten_list(seq[1:])
+
+
+def simplify_sympy_array(arr):
+    flattened_list = _flatten_list(arr.tolist())
+    simplified_flattened_list = [sympy.simplify(e) for e in flattened_list]
+    return sympy.Array(simplified_flattened_list, arr.shape)
diff --git a/src/einsteinpy/symbolic/ricci.py b/src/einsteinpy/symbolic/ricci.py
index f9049b2..c56ea1e 100644
--- a/src/einsteinpy/symbolic/ricci.py
+++ b/src/einsteinpy/symbolic/ricci.py
@@ -171,7 +171,7 @@ class RicciScalar(BaseRelativityTensor):
             arr=arr, syms=syms, config="", parent_metric=parent_metric
         )
         self._order = 0
-        self._expr = self.arr[0]
+        self._expr = sum(self.arr)  # sympy not allowing indexing, temporary fix!
 
     @property
     def expr(self):
diff --git a/src/einsteinpy/symbolic/tensor.py b/src/einsteinpy/symbolic/tensor.py
index a9da56b..831c0f2 100644
--- a/src/einsteinpy/symbolic/tensor.py
+++ b/src/einsteinpy/symbolic/tensor.py
@@ -4,6 +4,8 @@ from sympy import simplify, tensorcontraction, tensorproduct
 from sympy.core.expr import Expr
 from sympy.core.function import AppliedUndef, UndefinedFunction
 
+from einsteinpy.symbolic.auxillary_functions import simplify_sympy_array
+
 
 def _config_checker(config):
     # check if the string for config contains 'u' and 'l' only
@@ -51,11 +53,8 @@ def _change_config(tensor, metric, newconfig):
                     tensorcontraction(tensorproduct(met_dict[action], t), (1, 2 + i))
                 )
                 # reshuffle the indices
-                tmp = np.array(t).reshape(t.shape)
-                source, dest = (
-                    [p for p in range(len(t.shape))],
-                    [p for p in range(len(t.shape))],
-                )
+                tmp = np.array(t.tolist()).reshape(t.shape)
+                source, dest = list(range(len(t.shape))), list(range(len(t.shape)))
                 dest.pop(i)
                 dest.insert(0, i)
                 tmp = np.moveaxis(tmp, source, dest)
@@ -181,7 +180,8 @@ class Tensor:
             Simplified Tensor
 
         """
-        return sympy.simplify(self.tensor())
+        # return sympy.simplify(self.tensor())  # this used to work with older sympy versions
+        return simplify_sympy_array(self.tensor())
 
 
 class BaseRelativityTensor(Tensor):
@@ -338,3 +338,41 @@ class BaseRelativityTensor(Tensor):
             numeric_arr = sympy.lambdify(args, self.arr, np)
             arg_list = tuple(args)
         return arg_list, numeric_arr
+
+    def lorentz_transform(self, transformation_matrix):
+        """
+        Performs a Lorentz transform on the tensor.
+
+        Parameters
+        ----------
+            transformation_matrix : ~sympy.tensor.array.dense_ndim_array.ImmutableDenseNDimArray or list
+                Sympy Array or multi-dimensional list containing Sympy Expressions
+
+        Returns
+        -------
+            ~einsteinpy.symbolic.tensor.BaseRelativityTensor
+                lorentz transformed tensor(or vector)
+
+        """
+        tm = sympy.Array(transformation_matrix)
+        t = self.tensor()
+        for i in range(self.order):
+            if self.config[i] == "u":
+                t = simplify(tensorcontraction(tensorproduct(tm, t), (1, 2 + i)))
+            else:
+                t = simplify(tensorcontraction(tensorproduct(tm, t), (0, 2 + i)))
+            tmp = np.array(t.tolist()).reshape(t.shape)
+            source, dest = list(range(len(t.shape))), list(range(len(t.shape)))
+            dest.pop(i)
+            dest.insert(0, i)
+            tmp = np.moveaxis(tmp, source, dest)
+            t = sympy.Array(tmp)
+
+        return BaseRelativityTensor(
+            t,
+            syms=self.syms,
+            config=self.config,
+            parent_metric=None,
+            variables=self.variables,
+            functions=self.functions,
+        )
diff --git a/src/einsteinpy/symbolic/vector.py b/src/einsteinpy/symbolic/vector.py
index c74b5c9..478019a 100644
--- a/src/einsteinpy/symbolic/vector.py
+++ b/src/einsteinpy/symbolic/vector.py
@@ -75,3 +75,24 @@ class GenericVector(BaseRelativityTensor):
             new_tensor, self.syms, config=newconfig, parent_metric=metric
         )
         return new_obj
+
+    def lorentz_transform(self, transformation_matrix):
+        """
+        Performs a Lorentz transform on the vector.
+
+        Parameters
+        ----------
+            transformation_matrix : ~sympy.tensor.array.dense_ndim_array.ImmutableDenseNDimArray or list
+                Sympy Array or multi-dimensional list containing Sympy Expressions
+
+        Returns
+        -------
+            ~einsteinpy.symbolic.vector.GenericVector
+                lorentz transformed vector
+
+        """
+
+        t = super(GenericVector, self).lorentz_transform(transformation_matrix)
+        return GenericVector(
+            t.tensor(), syms=self.syms, config=self.config, parent_metric=None
+        )


Test patch:
diff --git a/src/einsteinpy/tests/test_symbolic/test_schouten.py b/src/einsteinpy/tests/test_symbolic/test_schouten.py
index 2576395..fa397fa 100644
--- a/src/einsteinpy/tests/test_symbolic/test_schouten.py
+++ b/src/einsteinpy/tests/test_symbolic/test_schouten.py
@@ -8,6 +8,7 @@ from einsteinpy.symbolic import (
     RicciTensor,
     RiemannCurvatureTensor,
     SchoutenTensor,
+    simplify_sympy_array,
 )
 
 
@@ -84,5 +85,5 @@ def test_SchoutenTensor_config_change():
     t3 = t2.change_config("ll")
     assert t1.config == "ll" and t3.config == "ll"
     compare = sympy.Array(np.zeros((4, 4), dtype=int))
-    testarr = sympy.simplify(t1.tensor() - t3.tensor())
+    testarr = simplify_sympy_array(t1.tensor() - t3.tensor())
     assert testarr == compare
diff --git a/src/einsteinpy/tests/test_symbolic/test_stress_energy_momentum.py b/src/einsteinpy/tests/test_symbolic/test_stress_energy_momentum.py
index 8508119..fc8689c 100644
--- a/src/einsteinpy/tests/test_symbolic/test_stress_energy_momentum.py
+++ b/src/einsteinpy/tests/test_symbolic/test_stress_energy_momentum.py
@@ -2,7 +2,11 @@ import numpy as np
 import sympy
 from sympy import cos, sin, sinh
 
-from einsteinpy.symbolic import MetricTensor, StressEnergyMomentumTensor
+from einsteinpy.symbolic import (
+    MetricTensor,
+    StressEnergyMomentumTensor,
+    simplify_sympy_array,
+)
 
 
 def schwarzschild_metric():
@@ -50,5 +54,5 @@ def test_StressEnergyMomentumTensor_cyclic_config_change():
     t3 = t2.change_config("ll")
     assert t1.config == "ll" and t3.config == "ll"
     compare = sympy.Array(np.zeros((4, 4), dtype=int))
-    testarr = sympy.simplify(t1.tensor() - t3.tensor())
+    testarr = simplify_sympy_array(t1.tensor() - t3.tensor())
     assert testarr == compare
diff --git a/src/einsteinpy/tests/test_symbolic/test_vector.py b/src/einsteinpy/tests/test_symbolic/test_vector.py
index ff56cc0..a7f5f50 100644
--- a/src/einsteinpy/tests/test_symbolic/test_vector.py
+++ b/src/einsteinpy/tests/test_symbolic/test_vector.py
@@ -1,5 +1,5 @@
 import numpy as np
-from sympy import cos, simplify, symbols
+from sympy import cos, cosh, simplify, sinh, symbols
 
 from einsteinpy.symbolic import GenericVector, MetricTensor
 
@@ -46,3 +46,28 @@ def test_GenericVector_check_ValueErrors():
     except ValueError:
         boolstore = True
     assert boolstore
+
+
+def test_lorentz_transform():
+    def get_vector():
+        syms = symbols("t x y z")
+        t, x, y, z = syms
+        return GenericVector([t, x, y, z], syms=syms, config="u")
+
+    def get_lorentz_matrix():
+        list2d = [[0 for t1 in range(4)] for t2 in range(4)]
+        phi = symbols("phi")
+        list2d[0][0], list2d[0][1], list2d[1][0], list2d[1][1] = (
+            cosh(phi),
+            -sinh(phi),
+            -sinh(phi),
+            cosh(phi),
+        )
+        list2d[2][2], list2d[3][3] = 1, 1
+        return list2d
+
+    t, x, phi = symbols("t x phi")
+    v = get_vector().lorentz_transform(get_lorentz_matrix())
+    print(v.tensor())
+    assert simplify(v[0] - (t * cosh(phi) - x * sinh(phi))) == 0
+    assert simplify(v[1] - (x * cosh(phi) - t * sinh(phi))) == 0
diff --git a/src/einsteinpy/tests/test_symbolic/test_weyl.py b/src/einsteinpy/tests/test_symbolic/test_weyl.py
index 4ac1cf3..9b7fc6d 100644
--- a/src/einsteinpy/tests/test_symbolic/test_weyl.py
+++ b/src/einsteinpy/tests/test_symbolic/test_weyl.py
@@ -8,6 +8,7 @@ from einsteinpy.symbolic import (
     RicciTensor,
     RiemannCurvatureTensor,
     WeylTensor,
+    simplify_sympy_array,
 )
 
 
@@ -85,7 +86,7 @@ def test_weyl_conformal_rescaling():
     w1 = WeylTensor.from_metric(mw1).change_config("ulll")
     w2 = WeylTensor.from_metric(mw2).change_config("ulll")
     cmp_arr = sympy.Array(np.zeros(shape=w1.tensor().shape, dtype=int))
-    assert sympy.simplify(w1.tensor() - w2.tensor()) == cmp_arr
+    assert simplify_sympy_array(w1.tensor() - w2.tensor()) == cmp_arr
     assert w1.syms == w1.symbols()
     assert w1.parent_metric == w1._parent_metric
 
@@ -93,7 +94,7 @@ def test_weyl_conformal_rescaling():
 def test_weyl_contraction_1st_3rd_indices_zero():
     mw1 = anti_de_sitter_metric()
     w1 = WeylTensor.from_metric(mw1)
-    t1 = sympy.simplify(
+    t1 = simplify_sympy_array(
         sympy.tensorcontraction(w1.change_config("ulll").tensor(), (0, 2))
     )
     t0 = sympy.Array(np.zeros(shape=t1.shape, dtype=int))