Repository: sagemath/sage

Issue:
Infinite recursion in `LinearMatrixGroup_generic`
### Is there an existing issue for this?

- [X] I have searched the existing issues for a bug report that matches the one I want to file, without success.

### Did you read the documentation and troubleshoot guide?

- [X] I have read the documentation and troubleshoot guide

### Environment

```markdown
- **OS**: ubuntu
- **Sage Version**: 9.5
```


### Steps To Reproduce

In some situation, the special linear groups `sage.groups.matrix_gps.linear.LinearMatrixGroup_generic` is  such that the two methods `order` (inherited from `sage/groups/group.pyx`) and `cardinality` (inherited from `sage/categories/finite_groups.py`) are calling each other
```python
q = 7
FqT.<T> = GF(q)[]
N = T^2+1
FqTN = QuotientRing(FqT, N*FqT)
S = SL(2,FqTN)
S.is_finite()
```
In particular it breaks `S.is_finite()` which should work out of the box given that the category of `S` is the category of finite groups (obtained by `S.category()`).

### Expected Behavior

```python
True
```

### Actual Behavior

```python
Traceback (most recent call last):
<ipython-input-11-397d936442b9> in <module>
----> 1 S.is_finite()

.../sage/groups/group.pyx in sage.groups.group.Group.is_finite (build/cythonized/sage/groups/group.c:3068)()
    188             NotImplementedError
    189         """
--> 190         return self.order() != infinity
    191 
    192     def is_multiplicative(self):

.../sage/groups/group.pyx in sage.groups.group.Group.order (build/cythonized/sage/groups/group.c:2924)()
    171         """
    172         try:
--> 173             return self.cardinality()
    174         except AttributeError:
    175             raise NotImplementedError

.../sage/categories/finite_groups.py in cardinality(self)
    106                 return self._cardinality_from_iterator()
    107             else:
--> 108                 return o()
    109 
    110         def some_elements(self):

... last 2 frames repeated, from the frame below ...

.../sage/groups/group.pyx in sage.groups.group.Group.order (build/cythonized/sage/groups/group.c:2924)()
    171         """
    172         try:
--> 173             return self.cardinality()
    174         except AttributeError:
    175             raise NotImplementedError

RecursionError: maximum recursion depth exceeded while calling a Python object
```

### Additional Information

_No response_


Discussion:

Resolving patch:
diff --git a/src/sage/categories/finite_groups.py b/src/sage/categories/finite_groups.py
--- a/src/sage/categories/finite_groups.py
+++ b/src/sage/categories/finite_groups.py
@@ -101,12 +101,9 @@ def cardinality(self):
                 sage: G.cardinality()
                 384
             """
-            try:
-                o = self.order
-            except AttributeError:
-                return self._cardinality_from_iterator()
-            else:
-                return o()
+            if hasattr(self, 'order'):
+                return self.order()
+            return self._cardinality_from_iterator()
 
         def some_elements(self):
             """
diff --git a/src/sage/groups/matrix_gps/linear.py b/src/sage/groups/matrix_gps/linear.py
--- a/src/sage/groups/matrix_gps/linear.py
+++ b/src/sage/groups/matrix_gps/linear.py
@@ -66,6 +66,8 @@
 from sage.groups.matrix_gps.named_group import (
     normalize_args_vectorspace, NamedMatrixGroup_generic)
 from sage.misc.latex import latex
+from sage.misc.misc_c import prod
+from sage.rings.infinity import Infinity
 
 
 ###############################################################################
@@ -307,3 +309,49 @@ def _check_matrix(self, x, *args):
         else:
             if x.determinant() == 0:
                 raise TypeError('matrix must non-zero determinant')
+
+    def order(self):
+        """
+        Return the order of ``self``.
+
+        EXAMPLES::
+
+            sage: G = SL(3, GF(5))
+            sage: G.order()
+            372000
+
+        TESTS:
+
+        Check if :trac:`36876` is fixed::
+
+            sage: SL(1, QQ).order()
+            1
+            sage: SL(2, ZZ).cardinality()
+            +Infinity
+
+        Check if :trac:`35490` is fixed::
+
+            sage: q = 7
+            sage: FqT.<T> = GF(q)[]
+            sage: N = T^2+1
+            sage: FqTN = QuotientRing(FqT, N*FqT)
+            sage: S = SL(2, FqTN)
+            sage: S.is_finite()
+            True
+            sage: S.order()
+            117600
+        """
+        n = self.degree()
+
+        if self.base_ring().is_finite():
+            q = self.base_ring().order()
+            ord = prod(q**n - q**i for i in range(n))
+            if self._special:
+                return ord / (q-1)
+            return ord
+
+        if self._special and n == 1:
+            return 1
+        return Infinity
+
+    cardinality = order


Test patch: