Repository: einsteinpy/einsteinpy

Issue:
Support substitution of variables in Tensor (symbolic module)
🐞 **Problem**
  - The title is self explanatory

📋  **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:
pep8speaks: Hello @ritzvik! Thanks for opening this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:

* In the file [`src/einsteinpy/symbolic/tensor.py`](https://github.com/einsteinpy/einsteinpy/blob/15e36d58745239e0ac9c1dddb2a57fac27a8b559/src/einsteinpy/symbolic/tensor.py):

> [Line 47:81](https://github.com/einsteinpy/einsteinpy/blob/15e36d58745239e0ac9c1dddb2a57fac27a8b559/src/einsteinpy/symbolic/tensor.py#L47): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (96 > 80 characters)
> [Line 53:81](https://github.com/einsteinpy/einsteinpy/blob/15e36d58745239e0ac9c1dddb2a57fac27a8b559/src/einsteinpy/symbolic/tensor.py#L53): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (115 > 80 characters)




codecov[bot]: # [Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=h1) Report
> Merging [#258](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=desc) into [master](https://codecov.io/gh/einsteinpy/einsteinpy/commit/03bad7d40d39bc43a00d10fe7d34592de7f9afc9?src=pr&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258/graphs/tree.svg?width=650&token=H3oDVYA11f&height=150&src=pr)](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #258      +/-   ##
==========================================
+ Coverage   94.14%   94.15%   +0.01%     
==========================================
  Files          28       28              
  Lines        1024     1026       +2     
==========================================
+ Hits          964      966       +2     
  Misses         60       60
```


| [Impacted Files](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/einsteinpy/symbolic/tensor.py](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258/diff?src=pr&el=tree#diff-c3JjL2VpbnN0ZWlucHkvc3ltYm9saWMvdGVuc29yLnB5) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=footer). Last update [03bad7d...ba947e8](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

codecov[bot]: # [Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=h1) Report
> Merging [#258](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=desc) into [master](https://codecov.io/gh/einsteinpy/einsteinpy/commit/03bad7d40d39bc43a00d10fe7d34592de7f9afc9?src=pr&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258/graphs/tree.svg?width=650&token=H3oDVYA11f&height=150&src=pr)](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #258      +/-   ##
==========================================
+ Coverage   94.14%   94.15%   +0.01%     
==========================================
  Files          28       28              
  Lines        1024     1026       +2     
==========================================
+ Hits          964      966       +2     
  Misses         60       60
```


| [Impacted Files](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/einsteinpy/symbolic/tensor.py](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258/diff?src=pr&el=tree#diff-c3JjL2VpbnN0ZWlucHkvc3ltYm9saWMvdGVuc29yLnB5) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=footer). Last update [03bad7d...ba947e8](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

codecov[bot]: # [Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=h1) Report
> Merging [#258](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=desc) into [master](https://codecov.io/gh/einsteinpy/einsteinpy/commit/03bad7d40d39bc43a00d10fe7d34592de7f9afc9?src=pr&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258/graphs/tree.svg?width=650&token=H3oDVYA11f&height=150&src=pr)](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #258      +/-   ##
==========================================
+ Coverage   94.14%   94.15%   +0.01%     
==========================================
  Files          28       28              
  Lines        1024     1026       +2     
==========================================
+ Hits          964      966       +2     
  Misses         60       60
```


| [Impacted Files](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/einsteinpy/symbolic/tensor.py](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258/diff?src=pr&el=tree#diff-c3JjL2VpbnN0ZWlucHkvc3ltYm9saWMvdGVuc29yLnB5) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=footer). Last update [03bad7d...ba947e8](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

codecov[bot]: # [Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=h1) Report
> Merging [#258](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=desc) into [master](https://codecov.io/gh/einsteinpy/einsteinpy/commit/03bad7d40d39bc43a00d10fe7d34592de7f9afc9?src=pr&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258/graphs/tree.svg?width=650&token=H3oDVYA11f&height=150&src=pr)](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #258      +/-   ##
==========================================
+ Coverage   94.14%   94.15%   +0.01%     
==========================================
  Files          28       28              
  Lines        1024     1026       +2     
==========================================
+ Hits          964      966       +2     
  Misses         60       60
```


| [Impacted Files](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/einsteinpy/symbolic/tensor.py](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258/diff?src=pr&el=tree#diff-c3JjL2VpbnN0ZWlucHkvc3ltYm9saWMvdGVuc29yLnB5) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=footer). Last update [03bad7d...ba947e8](https://codecov.io/gh/einsteinpy/einsteinpy/pull/258?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/docs/source/examples/Symbolically_Understanding_Christoffel_Symbol_and_Riemann_Curvature_Tensor_using_EinsteinPy.ipynb b/docs/source/examples/Symbolically_Understanding_Christoffel_Symbol_and_Riemann_Curvature_Tensor_using_EinsteinPy.ipynb
index 1af3a91..e3c163b 100644
--- a/docs/source/examples/Symbolically_Understanding_Christoffel_Symbol_and_Riemann_Curvature_Tensor_using_EinsteinPy.ipynb
+++ b/docs/source/examples/Symbolically_Understanding_Christoffel_Symbol_and_Riemann_Curvature_Tensor_using_EinsteinPy.ipynb
@@ -284,6 +284,81 @@
    "cell_type": "code",
    "execution_count": 8,
    "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/latex": [
+       "$$\\left[\\begin{matrix}1 & 0 & 0 & 0\\\\0 & - \\frac{1}{c^{2}} & 0 & 0\\\\0 & 0 & - \\frac{r^{2}}{c^{2}} & 0\\\\0 & 0 & 0 & - \\frac{r^{2} \\sin^{2}{\\left (\\theta \\right )}}{c^{2}}\\end{matrix}\\right]$$"
+      ],
+      "text/plain": [
+       "⎡1   0    0         0      ⎤\n",
+       "⎢                          ⎥\n",
+       "⎢   -1                     ⎥\n",
+       "⎢0  ───   0         0      ⎥\n",
+       "⎢     2                    ⎥\n",
+       "⎢    c                     ⎥\n",
+       "⎢                          ⎥\n",
+       "⎢          2               ⎥\n",
+       "⎢        -r                ⎥\n",
+       "⎢0   0   ────       0      ⎥\n",
+       "⎢          2               ⎥\n",
+       "⎢         c                ⎥\n",
+       "⎢                          ⎥\n",
+       "⎢                2    2    ⎥\n",
+       "⎢              -r ⋅sin (θ) ⎥\n",
+       "⎢0   0    0    ────────────⎥\n",
+       "⎢                    2     ⎥\n",
+       "⎣                   c      ⎦"
+      ]
+     },
+     "execution_count": 8,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# single substitution\n",
+    "subs1 = sch.subs(a,0)\n",
+    "subs1.tensor()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/latex": [
+       "$$\\left[\\begin{matrix}1 & 0 & 0 & 0\\\\0 & -1 & 0 & 0\\\\0 & 0 & - r^{2} & 0\\\\0 & 0 & 0 & - r^{2} \\sin^{2}{\\left (\\theta \\right )}\\end{matrix}\\right]$$"
+      ],
+      "text/plain": [
+       "⎡1  0    0        0     ⎤\n",
+       "⎢                       ⎥\n",
+       "⎢0  -1   0        0     ⎥\n",
+       "⎢                       ⎥\n",
+       "⎢         2             ⎥\n",
+       "⎢0  0   -r        0     ⎥\n",
+       "⎢                       ⎥\n",
+       "⎢              2    2   ⎥\n",
+       "⎣0  0    0   -r ⋅sin (θ)⎦"
+      ]
+     },
+     "execution_count": 9,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# multiple substitution\n",
+    "subs2 = sch.subs([(a,0), (c,1)])\n",
+    "subs2.tensor()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "metadata": {},
    "outputs": [
     {
      "data": {
@@ -355,7 +430,7 @@
        "    ⎦"
       ]
      },
-     "execution_count": 8,
+     "execution_count": 10,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -374,7 +449,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 9,
+   "execution_count": 11,
    "metadata": {
     "scrolled": false
    },
@@ -416,7 +491,7 @@
        "r)⋅sin (θ)⎦  ⎣             2     ⎦  ⎣   r  tan(θ)        ⎦⎦"
       ]
      },
-     "execution_count": 9,
+     "execution_count": 11,
      "metadata": {},
      "output_type": "execute_result"
     }
diff --git a/src/einsteinpy/symbolic/tensor.py b/src/einsteinpy/symbolic/tensor.py
index 053293a..3d88aba 100644
--- a/src/einsteinpy/symbolic/tensor.py
+++ b/src/einsteinpy/symbolic/tensor.py
@@ -42,6 +42,24 @@ class Tensor:
         """
         return self.arr
 
+    def subs(self, *args):
+        """
+        Substitute the variables/expressions in a Tensor with other sympy variables/expressions.
+
+        Parameters
+        ----------
+        args : one argument or two argument
+            - two arguments, e.g foo.subs(old, new)
+            - one iterable argument, e.g foo.subs([(old1, new1), (old2, new2)]) for multiple substitutions at once.
+
+        Returns
+        -------
+        ~einsteinpy.symbolic.tensor.Tensor:
+            Tensor with substituted values
+
+        """
+        return Tensor(self.tensor().subs(*args))
+
     def simplify(self):
         """
         Returns a simplified Tensor


Test patch:
diff --git a/src/einsteinpy/tests/test_symbolic/test_tensor.py b/src/einsteinpy/tests/test_symbolic/test_tensor.py
index 12239e3..fe650c7 100644
--- a/src/einsteinpy/tests/test_symbolic/test_tensor.py
+++ b/src/einsteinpy/tests/test_symbolic/test_tensor.py
@@ -1,8 +1,24 @@
-from sympy import Array, cos, sin, symbols
+import numpy as np
+from sympy import Array, cos, simplify, sin, symbols
 
 from einsteinpy.symbolic.tensor import Tensor
 
 
+def schwarzschild_tensor():
+    symbolstr = "t r theta phi"
+    syms = symbols(symbolstr)
+    G, M, c, a = symbols("G M c a")
+    # using metric values of schwarschild space-time
+    # a is schwarzschild radius
+    list2d = np.zeros((4, 4), dtype=int).tolist()
+    list2d[0][0] = 1 - (a / syms[1])
+    list2d[1][1] = -1 / ((1 - (a / syms[1])) * (c ** 2))
+    list2d[2][2] = -1 * (syms[1] ** 2) / (c ** 2)
+    list2d[3][3] = -1 * (syms[1] ** 2) * (sin(syms[2]) ** 2) / (c ** 2)
+    sch = Tensor(list2d)
+    return sch
+
+
 def test_Tensor():
     x, y, z = symbols("x y z")
     test_list = [[[x, y], [y, sin(2 * z) - 2 * sin(z) * cos(z)]], [[z ** 2, x], [y, z]]]
@@ -21,3 +37,22 @@ def test_TypeError():
         assert False
     except TypeError:
         assert True
+
+
+def test_subs_single():
+    # replacing only schwarzschild radius(a) with 0
+    T = schwarzschild_tensor()
+    a, c = symbols("a c")
+    test_arr = T.subs(a, 0)
+    assert simplify(test_arr.arr[0, 0] - 1) == 0
+    assert simplify(test_arr.arr[1, 1] - (-1 / c ** 2)) == 0
+
+
+def test_subs_multiple():
+    # replacing a with 0, c with 1
+    # this should give a metric for spherical coordinates
+    T = schwarzschild_tensor()
+    a, c = symbols("a c")
+    test_arr = T.subs([(a, 0), (c, 1)])
+    assert simplify(test_arr.arr[0, 0] - 1) == 0
+    assert simplify(test_arr.arr[1, 1] - (-1)) == 0