Repository: einsteinpy/einsteinpy

Issue:
Addition of Einstein Tensor to symbolic module 
🎯 **Goal**
* Add a class EinsteinTensor in src/einsteinpy/symbolic/einstein.py
* https://en.wikipedia.org/wiki/Einstein_tensor

📋  **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 @OVSofia! 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/einstein.py`](https://github.com/einsteinpy/einsteinpy/blob/f3df7dc279ef5d7eece048ef2b887a32d9624a60/src/einsteinpy/symbolic/einstein.py):

> [Line 23:121](https://github.com/einsteinpy/einsteinpy/blob/f3df7dc279ef5d7eece048ef2b887a32d9624a60/src/einsteinpy/symbolic/einstein.py#L23): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (134 > 120 characters)

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

> [Line 25:121](https://github.com/einsteinpy/einsteinpy/blob/f3df7dc279ef5d7eece048ef2b887a32d9624a60/src/einsteinpy/symbolic/weyl.py#L25): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (136 > 120 characters)
> [Line 99:29](https://github.com/einsteinpy/einsteinpy/blob/f3df7dc279ef5d7eece048ef2b887a32d9624a60/src/einsteinpy/symbolic/weyl.py#L99): [W503](https://duckduckgo.com/?q=pep8%20W503) line break before binary operator
> [Line 100:29](https://github.com/einsteinpy/einsteinpy/blob/f3df7dc279ef5d7eece048ef2b887a32d9624a60/src/einsteinpy/symbolic/weyl.py#L100): [W503](https://duckduckgo.com/?q=pep8%20W503) line break before binary operator
> [Line 101:29](https://github.com/einsteinpy/einsteinpy/blob/f3df7dc279ef5d7eece048ef2b887a32d9624a60/src/einsteinpy/symbolic/weyl.py#L101): [W503](https://duckduckgo.com/?q=pep8%20W503) line break before binary operator
> [Line 103:25](https://github.com/einsteinpy/einsteinpy/blob/f3df7dc279ef5d7eece048ef2b887a32d9624a60/src/einsteinpy/symbolic/weyl.py#L103): [W503](https://duckduckgo.com/?q=pep8%20W503) line break before binary operator
> [Line 105:21](https://github.com/einsteinpy/einsteinpy/blob/f3df7dc279ef5d7eece048ef2b887a32d9624a60/src/einsteinpy/symbolic/weyl.py#L105): [W503](https://duckduckgo.com/?q=pep8%20W503) line break before binary operator
> [Line 107:25](https://github.com/einsteinpy/einsteinpy/blob/f3df7dc279ef5d7eece048ef2b887a32d9624a60/src/einsteinpy/symbolic/weyl.py#L107): [W503](https://duckduckgo.com/?q=pep8%20W503) line break before binary operator
> [Line 108:25](https://github.com/einsteinpy/einsteinpy/blob/f3df7dc279ef5d7eece048ef2b887a32d9624a60/src/einsteinpy/symbolic/weyl.py#L108): [W503](https://duckduckgo.com/?q=pep8%20W503) line break before binary operator




codecov[bot]: # [Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/301?src=pr&el=h1) Report
> Merging [#301](https://codecov.io/gh/einsteinpy/einsteinpy/pull/301?src=pr&el=desc) into [master](https://codecov.io/gh/einsteinpy/einsteinpy/commit/b1f5ae429de504e2d15bd432e82987c4e7f7bbd2?src=pr&el=desc) will **decrease** coverage by `1.29%`.
> The diff coverage is `69.62%`.

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

```diff
@@            Coverage Diff            @@
##           master     #301     +/-   ##
=========================================
- Coverage   93.89%   92.59%   -1.3%     
=========================================
  Files          37       38      +1     
  Lines        1409     1486     +77     
=========================================
+ Hits         1323     1376     +53     
- Misses         86      110     +24
```


| [Impacted Files](https://codecov.io/gh/einsteinpy/einsteinpy/pull/301?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/einsteinpy/symbolic/\_\_init\_\_.py](https://codecov.io/gh/einsteinpy/einsteinpy/pull/301/diff?src=pr&el=tree#diff-c3JjL2VpbnN0ZWlucHkvc3ltYm9saWMvX19pbml0X18ucHk=) | `100% <100%> (ø)` | :arrow_up: |
| [src/einsteinpy/symbolic/einstein.py](https://codecov.io/gh/einsteinpy/einsteinpy/pull/301/diff?src=pr&el=tree#diff-c3JjL2VpbnN0ZWlucHkvc3ltYm9saWMvZWluc3RlaW4ucHk=) | `29.03% <26.66%> (-70.97%)` | :arrow_down: |
| [src/einsteinpy/symbolic/weyl.py](https://codecov.io/gh/einsteinpy/einsteinpy/pull/301/diff?src=pr&el=tree#diff-c3JjL2VpbnN0ZWlucHkvc3ltYm9saWMvd2V5bC5weQ==) | `95.83% <95.83%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/301?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/301?src=pr&el=footer). Last update [b1f5ae4...f3df7dc](https://codecov.io/gh/einsteinpy/einsteinpy/pull/301?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/301?src=pr&el=h1) Report
> Merging [#301](https://codecov.io/gh/einsteinpy/einsteinpy/pull/301?src=pr&el=desc) into [master](https://codecov.io/gh/einsteinpy/einsteinpy/commit/b1f5ae429de504e2d15bd432e82987c4e7f7bbd2?src=pr&el=desc) will **decrease** coverage by `1.29%`.
> The diff coverage is `69.62%`.

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

```diff
@@            Coverage Diff            @@
##           master     #301     +/-   ##
=========================================
- Coverage   93.89%   92.59%   -1.3%     
=========================================
  Files          37       38      +1     
  Lines        1409     1486     +77     
=========================================
+ Hits         1323     1376     +53     
- Misses         86      110     +24
```


| [Impacted Files](https://codecov.io/gh/einsteinpy/einsteinpy/pull/301?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/einsteinpy/symbolic/\_\_init\_\_.py](https://codecov.io/gh/einsteinpy/einsteinpy/pull/301/diff?src=pr&el=tree#diff-c3JjL2VpbnN0ZWlucHkvc3ltYm9saWMvX19pbml0X18ucHk=) | `100% <100%> (ø)` | :arrow_up: |
| [src/einsteinpy/symbolic/einstein.py](https://codecov.io/gh/einsteinpy/einsteinpy/pull/301/diff?src=pr&el=tree#diff-c3JjL2VpbnN0ZWlucHkvc3ltYm9saWMvZWluc3RlaW4ucHk=) | `29.03% <26.66%> (-70.97%)` | :arrow_down: |
| [src/einsteinpy/symbolic/weyl.py](https://codecov.io/gh/einsteinpy/einsteinpy/pull/301/diff?src=pr&el=tree#diff-c3JjL2VpbnN0ZWlucHkvc3ltYm9saWMvd2V5bC5weQ==) | `95.83% <95.83%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/301?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/301?src=pr&el=footer). Last update [b1f5ae4...f3df7dc](https://codecov.io/gh/einsteinpy/einsteinpy/pull/301?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

OVSofia: About the test, I was looking for testing the property of divergenless, but I am not sure on how calculate it. I have check and maybe CovarDerivativeOp could be usefull, but I'm not sure about how to use it :/ . Do you know another option for the test?
ritzvik: This property can used to test https://en.wikipedia.org/wiki/Einstein_tensor#Trace

Patch:
diff --git a/docs/source/api/symbolic/einstein.rst b/docs/source/api/symbolic/einstein.rst
new file mode 100644
index 0000000..8227edd
--- /dev/null
+++ b/docs/source/api/symbolic/einstein.rst
@@ -0,0 +1,8 @@
+Einstein Tensor Module
+======================
+
+This module contains the class for obtaining Einstein Tensor
+related to a Metric belonging to any arbitrary space-time symbolically:
+
+.. automodule:: einsteinpy.symbolic.einstein
+    :members:
diff --git a/docs/source/api/symbolic/symbolic_index.rst b/docs/source/api/symbolic/symbolic_index.rst
index 5993f69..ba21f96 100644
--- a/docs/source/api/symbolic/symbolic_index.rst
+++ b/docs/source/api/symbolic/symbolic_index.rst
@@ -13,4 +13,5 @@ calculations related to General Relativity.
     christoffel
     riemann
     ricci
-    weyl
\ No newline at end of file
+    weyl
+    einstein
\ No newline at end of file
diff --git a/src/einsteinpy/symbolic/einstein.py b/src/einsteinpy/symbolic/einstein.py
index 2129961..e6603d1 100644
--- a/src/einsteinpy/symbolic/einstein.py
+++ b/src/einsteinpy/symbolic/einstein.py
@@ -1,5 +1,112 @@
-from .tensor import Tensor
+import sympy
+
+from einsteinpy.symbolic.ricci import RicciScalar, RicciTensor
+from einsteinpy.symbolic.tensor import Tensor, _change_config
 
 
 class EinsteinTensor(Tensor):
-    pass
+    """
+    Class for defining Einstein Tensor
+    """
+
+    def __init__(self, arr, syms, config="ll", parent_metric=None):
+        """
+        Constructor and Initializer
+
+        Parameters
+        ----------
+        arr : ~sympy.tensor.array.dense_ndim_array.ImmutableDenseNDimArray or list
+            Sympy Array or multi-dimensional list containing Sympy Expressions
+        syms : tuple or list
+            Tuple of crucial symbols denoting time-axis, 1st, 2nd, and 3rd axis (t,x1,x2,x3)
+        config : str
+            Configuration of contravariant and covariant indices in tensor. 'u' for upper and 'l' for lower indices. Defaults to 'll'.
+        parent_metric : ~einsteinpy.symbolic.metric.MetricTensor or None
+            Corresponding Metric for the Einstein Tensor.
+            Defaults to None.
+
+        Raises
+        ------
+        TypeError
+            Raised when arr is not a list or sympy Array
+        TypeError
+            syms is not a list or tuple
+        ValueError
+            config has more or less than 2 indices
+
+        """
+        super(EinsteinTensor, self).__init__(arr, config=config)
+        self._order = 2
+        self._parent_metric = parent_metric
+        if isinstance(syms, (list, tuple)):
+            self.syms = syms
+            self.dims = len(self.syms)
+        else:
+            raise TypeError("syms should be a list or tuple")
+        if not len(config) == self._order:
+            raise ValueError("config should be of length {}".format(self._order))
+
+    @property
+    def parent_metric(self):
+        """
+        Returns the Parent Metric, if available.
+        """
+        return self._parent_metric
+
+    @classmethod
+    def from_metric(cls, metric):
+        t_ricci = RicciTensor.from_metric(metric)
+        r_scalar = RicciScalar.from_riccitensor(t_ricci, t_ricci.parent_metric)
+        einstein_tensor = t_ricci.tensor() - (1 / 2) * metric.tensor() * r_scalar.expr
+        return cls(
+            einstein_tensor,
+            metric.syms,
+            config="ll",
+            parent_metric=t_ricci.parent_metric,
+        )
+
+    def change_config(self, newconfig="ul", metric=None):
+        """
+        Changes the index configuration(contravariant/covariant)
+
+        Parameters
+        ----------
+        newconfig : str
+            Specify the new configuration. Defaults to 'ul'
+        metric : ~einsteinpy.symbolic.metric.MetricTensor or None
+            Parent metric tensor for changing indices.
+            Already assumes the value of the metric tensor from which it was initialized if passed with None.
+            Compulsory if somehow does not have a parent metric. Defaults to None.
+
+        Returns
+        -------
+        ~einsteinpy.symbolic.einstein.EinsteinTensor
+            New tensor with new configuration. Defaults to 'ul'
+
+        Raises
+        ------
+        Exception
+            Raised when a parent metric could not be found.
+
+        """
+        if metric is None:
+            metric = self._parent_metric
+        if metric is None:
+            raise Exception("Parent Metric not found, can't do configuration change")
+        new_tensor = _change_config(self, metric, newconfig)
+        new_obj = EinsteinTensor(
+            new_tensor, self.syms, config=newconfig, parent_metric=metric
+        )
+        return new_obj
+
+    def symbols(self):
+        """
+        Returns the symbols used for defining the time & spacial axis
+
+        Returns
+        -------
+        tuple
+            tuple containing (t,x1,x2,x3)
+
+        """
+        return self.syms


Test patch:
diff --git a/src/einsteinpy/tests/test_symbolic/test_einstein.py b/src/einsteinpy/tests/test_symbolic/test_einstein.py
new file mode 100644
index 0000000..a68d75d
--- /dev/null
+++ b/src/einsteinpy/tests/test_symbolic/test_einstein.py
@@ -0,0 +1,68 @@
+import numpy as np
+import pytest
+import sympy
+from sympy import cos, simplify, sin, sinh, tensorcontraction
+
+from einsteinpy.symbolic import EinsteinTensor, MetricTensor, RicciScalar
+
+
+def schwarzschild_metric():
+    symbolstr = "t r theta phi"
+    syms = sympy.symbols(symbolstr)
+    G, M, c, a = sympy.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) * (sympy.sin(syms[2]) ** 2) / (c ** 2)
+    sch = MetricTensor(list2d, syms)
+    return sch
+
+
+def anti_de_sitter_metric():
+    coords = sympy.symbols("t chi theta phi")
+    t, ch, th, ph = coords
+    m = sympy.diag(
+        -1,
+        cos(t) ** 2,
+        cos(t) ** 2 * sinh(ch) ** 2,
+        cos(t) ** 2 * sinh(ch) ** 2 * sin(th) ** 2,
+    ).tolist()
+    metric = MetricTensor(m, coords)
+    return metric
+
+
+def test_EinsteinTensor_ValueError_wrong_config_length():
+    testarr = np.ones((4, 4), dtype=int).tolist()
+    syms = sympy.symbols("x y")
+    try:
+        obj = EinsteinTensor(testarr, syms, config="lll", parent_metric=None)
+        assert False
+    except ValueError:
+        assert True
+
+
+@pytest.mark.parametrize("metric", [anti_de_sitter_metric(), schwarzschild_metric()])
+def test_EinsteinTensor_trace_negetive_of_Ricci_Scalar_in_4D(metric):
+    # https://en.wikipedia.org/wiki/Einstein_tensor#Trace
+    G1 = EinsteinTensor.from_metric(metric)
+    G2 = G1.change_config("ul")
+    val1 = simplify(tensorcontraction(G2.tensor(), (0, 1)))
+    val2 = RicciScalar.from_metric(metric).expr
+    assert simplify(val1 + val2) == 0
+
+
+def test_EinsteinTensor_symbols_parent_metric_wrong_change_config():
+    metric = anti_de_sitter_metric()
+    G = EinsteinTensor.from_metric(metric)
+    assert G.parent_metric == G._parent_metric and G.parent_metric == metric
+    assert G.symbols() == G.syms and G.symbols() == metric.symbols()
+    G._parent_metric = None
+    try:
+        obj = G.change_config("ul")
+        boolstore = False
+    except Exception:
+        boolstore = True
+    assert boolstore