> 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/trigoexpr.md).

# TrigoExpr

\*experimental feature

```python
class TrigoExpr(IExpression, IPlottable, IScatterable):
                    
```

The `TrigoExpr` class represents a trigonometric expression. It is still under development, but it already supports relatively advanced features with ease. For instance, it integrates relatively well with other types of expressions, it supports basic arithmetic operations, it can compute simple cases of derivatives and some common trigonometric identities. This feature will be heavily extended in the future or modified, to be even more robust.

#### Creating a new instance

There are several ways to create a `TrigoExpr` object.

1. Entering a string. For example:

   ```python
   print(TrigoExpr('sin(3x)', dtype='poly'))
   print(TrigoExpr('sin(log(2x+5))', dtype='log'))
                               
   ```

   Output:

   ```python
   sin(3x)
   sin(log10(2x+5))
                               
   ```
2. Using subclasses - `TrigoExpr` has many subclasses that make it more intuitive to create new trigonometric expressions:

   * `Sin`
   * `Cos`
   * `Tan`
   * `Cot`
   * `Sec`
   * `Csc`
   * `Asin`
   * `Acos`
   * `Atan`

   You have two main ways to create new objects via these subclasses: Entering a string that represents the internal expression and its type, or alternatively entering the algebraic expression directly. For instance:

   ```python
   my_sin = Sin('2x', dtype='poly')
   my_cos = Cos('log(3x)', dtype='log')
                               
   ```

   ```
   x = Var('x')
   my_sin = Sin(2*x)
   my_cos = Sin(Log(3*x))
                               
   ```

#### Addition

You can add `TrigoExpr` objects with the `+` operator. For instance:

```python
import math
x = Var('x')
print(Sin(x) + Sin(x))
print(Sin(x) + Cos(x))
print(Sin(math.pi/2) + 4)
                    
```

#### Subtraction

You can subtract `TrigoExpr` objects with the `-` operator. For instance:

```python
import math
x = Var('x')
print(Cos(x) - 2*Sin(x))
print(2*Sin(x) - Sin(x))
print(3*Sin(math.pi/2) - 2)
                    
```

#### Multiplication

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

```python
x = Var('x')
print(Sin(x)*Cos(x))
print(5*Sin(2*x))
print(3*x**2 * Tan(Log(x)))
                    
```

Output:

```python
sin(x)*cos(x)
5sin(2x)
1*(3x^2)*tan(log10(x))
                    
```

#### Division

You can divide `TrigoExpr` objects via the `/` operator. For example:

```python
x = Var('x')
print(Sin(x)/Cos(x))
print(2*Sin(x)*Cos(x) / Sin(2*x) )
print((3*x*Sin(x)) / Log(x))

                    
```

#### Power

#### Equating

You can check equality between two `TrigoExpr` expressions via the `==` operator. However, checking equality between trigonometric expressions is highly problematic, considering trigonometric identities. For instance, $$cos(x) = cos(x+2\pi)$$, $$sin(2x) = 2sin(x)cos(x)$$ The algorithm for this method doesn't take into consideration all of the relevant identities, and therefore there might be false negatives. However, there is little to none chance of false positives occurring. Here are some examples:

```python
x = Var('x')
print(Sin(2*x) == Sin(2*x))
print(Sin(x) == Sin(x+2*math.pi))
print(2*Sin(x)*Cos(x) == Sin(2*x))
print(Sin(x) == Sin(x+5))
                    
```

#### Simplify

This method is used mainly internally, but can also be used manually by the user, in order to clean the expression from negligible expressions. For instance, the expression $$sin(x)^2\*cos(2x)^0$$ can be simplified to just $$sin(x)^2$$.

#### `to_lambda()`

You can generate an executable lambda expression from the `TrigoExpr` object via the `to_lambda()` method. For instance:

```python
import math
x = Var('x')
my_lambda = Sin(x).to_lambda()
print(my_lambda(math.pi/2))
                    
```

Output:

```bash
1.0
                    
```

#### Evaluate to `int` or `float`

You can try to evaluate the expression into `int` or `float` via the `try_evaluate()` method. For instance, the expression \`sin(\frac(\pi)(2))\` can be evaluated into \`1\`. If the expression can't be evaluated, `None` will be returned. For example:

```python
import math
x = Var('x')
my_trigo = Sin(math.pi)
my_eval = my_trigo.try_evaluate()
if my_eval is not None:
    print("the expression could be evaluated")
else:
    print("the expression couldn't be evaluated")
```

#### newton

You can use newton's method in order to find a root of the trigonometric expression. For example:

```python
x = Var('x')
print(Sin(x).newton(initial_value=2))
                    
```

#### `reinman()`

You can use the Reinman Sum Method with via the `reinman()` method in order to find the finite integral of the trigonometric function. For instance:

```python
import math
x = Var('x')
print(Sin(x).reinman(0, math.pi, 20))
                    
```

```
1.9954413183201944
                    
```

#### `trapz()`

You can also use the trapezoid rule via the `trapz()` method in order to find the finite integral of the trigonometric function. For example:

```python
import math
x = Var('x')
print(Sin(x).trapz(0, math.pi, 20))
                    
```

Output:

```python
1.9958859727087144
                    
```

Here we also see that $$\int\_0^\pi \sin(x) , dx = 2$$

#### `simpson()`

You can also use Simpson's method in order to find the finite integral:

```python
import math
x = Var('x')
print(Sin(x).simpson(0, math.pi, 20))
                    
```

Output:

```python
1.9863695787474476
              
```
