/computorv2

We have bc at home

Primary LanguagePythonGNU General Public License v3.0GPL-3.0

๐Ÿ–ฅ๏ธ Computor v2

This project is the direct continuation of computorv1.

Computor v2 is a program that works like an advanced calculator. It can evaluate complex expressions, solve equations, store variables and functions... It has been written as part of the 42 school curriculum.

My motivation was drastically reduced during the project due to the few experience points gained for the time invested. 42, my programming school, considers that a simple network project completed in a few hours yields 22450 XP compared to 9450 XP for Computor v2. This project took me hundreds of hours... I learned what I wanted. This is why I did not want to finish all the features on the optimizer. However, I left the document that I had written on the rules to apply if necessary. GLHF ๐Ÿ™‚

โœจ Features

๐Ÿ“š Core features

  • โŒจ๏ธ A CLI that allows the user to interact with the program
  • ๐Ÿงฎ An evaluator with support for:
    • ๐Ÿ“Š Complex numbers
    • ๐Ÿ“ Matrices
    • ๐Ÿ“ Arithmetic operations
    • ๐Ÿ’พ Functions and variables assignment
    • ๐Ÿ“ˆ Equations solving of degree up to 2

๐ŸŽ Bonus features

  • ๐Ÿ“œ Auto-completion for functions and variables names
  • ๐Ÿ’ฝ Commands history
  • ๐Ÿงฑ Built-in constants:
    • e - Euler's number
    • i - Imaginary unit
    • inf - Infinity
    • pi - Pi
    • tau - Tau
  • ๐Ÿงฑ Built-in functions:
    • abs - Absolute value
    • cos - Cosine
    • exp - Exponential
    • identity - Identity matrix
    • inv - Inverse matrix
    • log - Natural logarithm
    • ones - Matrix of ones
    • sin - Sine
    • sqrt - Square root
    • tan - Tangent
    • transpose - Transpose matrix
    • zeros - Matrix of zeros
  • โŒจ๏ธ Built-in commands with prefix %:
    • clear - ๐Ÿงน Clear the screen
    • delete - ๐Ÿ—‘๏ธ Delete a variable or a function
    • help - ๐Ÿ“š Display the list of available commands or the help of a specific command
    • show - ๐Ÿ“œ Display all stored functions and/or variables
  • ๐Ÿ“ˆ Functions with multiple arguments

๐Ÿš€ Improvements

  • Expressions evaluation can be done without using = ? at the end of the line.
  • Support for digits and underscores in function and variable names
  • Matrix exponentiation use the exponentiation by squaring algorithm for better performance
  • The commands history is saved in a file to be persistent between sessions
  • The program can be run in a non-interactive mode by providing a file as a shell redirection like this:
     ./computorv2 < file.txt

๐Ÿ“ฆ Prerequisites

Python 3.9.2 or higher is recommended to run the program. An additional package named PLY is also required. You can install it by running the following command:

pip3 install -r requirements.txt

๐Ÿ› ๏ธ Usage

To use the program, run the following command:

make run

Then, you can enter your expressions in the prompt that appears.

๐Ÿ’ก Tips

  • ๐Ÿ”ฌ Some examples are available in the example directory.
  • โš ๏ธ Implicit multiplication has a higher priority than every other operation, so:
    • 2x ^ 3 is evaluated as (2 * x) ^ 3
    • 2 * x ^ 3 is evaluated as 2 * (x ^ 3)
  • ๐Ÿ’ก You can use the %help command to display the list of available commands.
  • ๐Ÿ’ก You can use the up and down arrow keys to navigate through the commands history.
  • ๐Ÿ’ก You can exit the program by pressing Ctrl + D.

๐Ÿงช Testing

The source code is covered by unit tests written using the unittest framework provided by Python. There are located in the test directory. The CLI is the only part of the program that is not covered by tests.

To run the unit tests, you can use the following command:

make test

โš–๏ธ License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.