> For the complete documentation index, see [llms.txt](https://jona-projects.gitbook.io/kiwicalc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jona-projects.gitbook.io/kiwicalc/symbolic-computation/factorial.md).

# Factorial

You can represent factorial expressions in the form $$a(x!)^n$$ , where $$a,n \in \mathbb{R}$$ via the `Factorial` class.

#### Properties

1. `coefficient` - the coefficient of the expression (algebraic expression or number)
2. `expression` - the internal expression that the factorial is applied to (algebraic expression or number)
3. `power` - algebraic expression or number.

#### Creating a new instance

You can create a new Factorial expression in two main ways via the `__init__()` method:

```python
 def __init__(self, expression: Optional[Union[IExpression, int, float, str]],
            coefficient: Union[IExpression, int, float] = Mono(1),
            power: Union[IExpression, int, float] = Mono(1), dtype='poly'):
```

* Enter the parameters directly - You can specify 3 parameters: the internal expression, the coefficient of the expression, and the power. Both the coefficient and the power will be 1 if you don't specify them, but you have to specify the internal expression. For example:

  ```python
  x = Var('x')
  my_factorial = Factorial(x**2 + 6*x + 8, power = 2)
  print(my_factorial)
  ```

  The output is:

  ```bash
  ((x^2+6x+8)!)**2                         
  ```
* Enter a string - you can enter a string that represents the expression. You also need to specify the datatype of the expressions inside. The default is `'poly'`. For example:

  ```python
  my_factorial = Factorial('(3x+5)!', dtype='poly')                         
  ```

#### Addition

You can add `Factorial` objects via the `+` operator. For instance:

```python
x = Var('x')
print(Factorial(x) + Factorial(x))
print(Factorial(5)+Factorial(4))
                        
```

#### Subtraction

You can subtract `Factorial` objects via the `-` operator. For instance:

```python
x = Var('x')
print(Factorial(x) - 0.5*Factorial(x))
print(Factorial(5)-Factorial(4))
                        
```

#### Multiplication

You can multiply `Factorial` objects via the `*` operator. For instance:

```python
x = Var('x')
print(Factorial(x)*Factorial(2*x))
print(2*x*Factorial(Sin(x)))
print(5*Factorial(1))
                    
```

#### Division

You can divide Factorial objects via the `/` operator. For instance:

```python
x = Var('x')
print(2*Factorial(x)/Factorial(x))
print(Factorial(3*x+4)/Factorial(2*x-1))
                    
```

#### Power

```python
x = Var('x')
print(Factorial(x) ** 2)
                    
```

#### Evaluate to float or int

You can evaluate the expression to `int` or `float` via the `try_evaluate()` method. If not possible, `None` will be returned. For example:

```python
my_factorial = Factorial(5)
my_eval = my_factorial.try_evaluate()
print(f"the evaluation is {my_eval} and its type is {type(my_eval)}")
                    
```

#### Assign values

You can assign values via the `assign()` method. For example:

```python
x = Var('x')
my_factorial = Factorial(x+2)
my_factorial.assign(x=2)
print(my_factorial)
                    
```

#### Plot

You can plot the `Factorial` object in 2D or 3D via the `plot()` method. For instance:

```python
x = Var('x')
my_factorial = Factorial(Sin(x))
my_factorial.plot(start=-5, stop=5, step=0.01)
                    
```

#### Scatter

You can also scatter the `Factorial` object. For example:

```python
x = Var('x')
my_factorial = Factorial(Sin(x))
my_factorial.plot(start=-5, stop=5, step=0.01)
                    
```
