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

# Mono

This class is used for representing a single polynomial expression, such as $$3x^2$$,  $$6x$$ etc. These types of expressions are called **Monomials**, and thus the class name.  In order to use this class, you must import it first:&#x20;

```python
from kiwicalc import Mono
```

### Creating a new Mono object

There are several main ways to create a Mono object:

**The first approach:**

You need to enter \`2\` parameters:

* The coefficient of the expression, should be a float
* A dictionary that contains the variables\_dict' name and exponents. Each key must be unique!

For example:

```python
expression = Mono(5, {'x': 2, 'y': 3})
print(expression)

# output:
# 5x^2*y^3
```

**The second approach:**

You can also enter a string, such as "3x^2\*y^4". This approach is more intuitive, however, it is slower in performance.

```python
expression = Mono("3x^2*y^4")
print(expression)
# output:
# '3x^2*y^4'
                    
```

**The third approach:**

If you wish to just create a free number, like \`5\`, \`9.6\`, etc, enter it as the coefficient:

```python
four = Mono(4)
print(four)

# output:
# 4

```

**The fourth approach ( shorter, but slower )**

You can actually create a `Mono` object using the `Var` class. For example:

```python
x = Var('x')
mono_expression = 3*x**2
```

In the example below we defined a variable, and created a single expression \`3x^2\` This expression will be automatically evaluated into a Mono object. This can be done with as many variables\_dict as you wish, and will be covered more thoroughly in the section regarding the `Var` class.

This approach is quite intuitive and simple, but also more costly in runtime. This is because \`x\` is first brought to power by \`2\`, which creates a new copy of Mono object, and then it's multiplied by \`3\`, which creates another object. So here instead of creating one object, we created 3 objects : \`x\`, \`x^2\`, and \`3x^2\`. Hopefully, that was a clear enough explanation about the inside mechanism, which I will hopefully modify in future versions.

#### Arithmetic Operators

As it will be further elaborated in the parts regarding the `Var` and `Poly` classes, you can perform addition,subtraction,multiplication and division, using their corresponding operators in python.\
Lets define two `Mono` objects:

```python
first_mono, second_mono = Mono("3x^2"), Mono("2x^2")
print(first_mono+second_mono)
print(first_mono - second_mono)
print(first_mono * second_mono)
print(first_mono / second_mono)
```

#### Assigning values

Suppose you have the monomial `3x^2`, and you wish to assign `x=5` to it. After x is assigned, the expression turns to `75`.\
You can assign numbers to variables\_dict by using the `assign()` method from an instance of a Mono object. In order to use this method, you must enter keyword arguments.\
For example:

```python
m = Mono(coefficient=3, variables_dict = {'x':2, 'y':1}) # 3x^2*y
m.assign(x=4)
print(m)
m.assign(y=3)
print(m)
# output:
# 48y
# 144
                    
```

You can also assign several variables\_dict in the same time:

```python
m.assign(x=4, y=3)
                    
```

If you want to assign a variable to the expression and get the result without changing the original expression, you can use the `when()` method.\
For example:

<pre class="language-python"><code class="lang-python"><strong>                        
</strong>m = Mono(coefficient=3, variables_dict = {'x':2, 'y':1}) # creating the expression
assigned_expression = m.when(x=4, y=3) # saving the assigned expression without modifying the original.
print(assigned_expression)
print(m)

output:
144
3x^2*y
                    
</code></pre>

#### Evaluation into int or float

Sometimes the monomials we deal with will only represent numbers, So we might want to convert them to a float number. In order to do that, we can use the `try_evaluate()` method. This is the signature of the method - it accepts no parameters, and returns None if the expression can't be evaluated to a number.

```python
def try_evaluate(self) -> Optional[float]:
                    
```

For instance,

```python
m = Mono(5)
print(m.try_evaluate())
                    
```

Another example: lets create an expression, assign a value to it so it becomes a number, and then fetch the float value with the aforementioned method.

<pre class="language-python"><code class="lang-python">m = Mono(coefficient=2, variables_dict = {'x':4}) # creating 2*x^4
<strong>assigned_expression = m.when(x=3) # assigning x=3 without changing the original object
</strong>
print(f"Value is {assigned_expression} and the type is {type(assigned_expression)}")
<strong>
</strong><strong>evaluated_number = assigned_expression.try_evaluate()
</strong>print(f"Value is {evaluated_number} and type is {type(evaluated_number)}")

# output:
# Value is 162 and the type is &#x3C;class '__main__.Mono'>
# Value is 162 and type is &#x3C;class 'int'>
</code></pre>

From the output of the program, you can understand that to get an integer or a float out of the object, we must call `try_evaluate()`.

#### Plot

You can plot `Mono` objects via the `plot()` method. For a a deeper dive into the method, go to [the relevant section](broken://pages/pTRMHHLCs8F75wD3kRE1) in the `IExpression` class. For example:

```python
my_mono = x ** 2
my_mono.plot()
                    
```

#### Scatter

You can scatter `Mono` objects via the `scatter()`r method. For a a deeper dive into the method, go to [the relevant section](broken://pages/pTRMHHLCs8F75wD3kRE1) in the `IExpression` class. For example:

```python
my_mono = x ** 2
my_mono.scatter()
                    
```
