Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Nonanti/mathcore/llms.txt

Use this file to discover all available pages before exploring further.

MathCore includes a built-in ASCII plotter that renders any single-variable mathematical expression as a text-based chart, directly in your terminal. This is useful for quick visual sanity-checks during development, exploring function behavior, and displaying results in environments where a graphical UI is unavailable — such as CI pipelines, SSH sessions, or embedded debug consoles.

MathCore::plot_ascii

pub fn plot_ascii(
    expression: &str,
    var: &str,
    x_min: f64,
    x_max: f64,
    width: usize,
    height: usize,
) -> Result<String, MathError>

Parameters

ParameterTypeDescription
expression&strThe mathematical expression to plot, e.g. "sin(x)".
var&strThe name of the x-axis variable used in the expression.
x_minf64The left bound of the x range.
x_maxf64The right bound of the x range.
widthusizeNumber of character columns in the output grid. Controls x-axis resolution.
heightusizeNumber of character rows in the output grid. Controls y-axis resolution.
Returns: Result<String, MathError> — the complete ASCII plot as a multi-line string, ready to be passed to println!. Returns Err if the expression cannot be parsed or if no finite points can be computed in the given range.

Basic Example

use mathcore::MathCore;

let plot = MathCore::plot_ascii("sin(x)", "x", -3.14, 3.14, 60, 20).unwrap();
println!("{}", plot);
This renders a sine wave over one full period using a 60-column by 20-row grid.

Output Format

The returned string has the following structure:
  1. Header line — shows the expression and the x range:
    Plot of sin(x) from x=-3.14 to x=3.14
    
  2. y-range line — the automatically-determined minimum and maximum y values:
    y range: [-1.00, 1.00]
    
  3. Blank line — separates the header from the grid.
  4. The plot grid — a rectangular character grid where:
    • | is drawn along the leftmost column as the y-axis.
    • - is drawn along the bottom row as the x-axis.
    • + marks the bottom-left corner where the axes meet.
    • * marks each evaluated point of the function.
Example output (abbreviated):
Plot of sin(x) from x=-3.14 to x=3.14
y range: [-1.00, 1.00]

|                              *
|                         *       *
|                       *           *
|                      *             *
|                     *               *
|    *               *
|  *   *           *
| *     *         *
|*       *       *
|         *     *
|          *   *
|            *
+------------------------------------------------------------
The y-axis range is computed automatically from the actual evaluated points — you do not need to specify it. The grid maps the minimum y value to the bottom row and the maximum y value to the top row.

More Examples

Parabola

use mathcore::MathCore;

let plot = MathCore::plot_ascii("x^2", "x", -3.0, 3.0, 50, 15).unwrap();
println!("{}", plot);
A parabola opens upward symmetrically. The vertex (at x=0, y=0) appears at the bottom of the grid, and the two arms curve upward toward the corners.

Cosine Wave

use mathcore::MathCore;

let plot = MathCore::plot_ascii("cos(x)", "x", 0.0, 6.28, 60, 20).unwrap();
println!("{}", plot);
With x_min=0 and x_max=6.28 (approximately tau), this plots a full period of the cosine function, starting at its maximum value of 1.

Combining with the Demo

The examples/demo.rs file uses plot_ascii with a 40-column, 15-row grid:
use mathcore::MathCore;

let plot = MathCore::plot_ascii("x^2", "x", -2.0, 2.0, 40, 15).unwrap();
println!("{}", plot);

Resolution Tips

Increase width for finer x-axis sampling — MathCore evaluates the function at width + 1 evenly-spaced x values across [x_min, x_max]. A wider grid reveals more horizontal detail.
Increase height for finer y-axis resolution. Each y value is mapped to the nearest row by linearly scaling between y_min and y_max. A taller grid reduces vertical quantisation.
A reasonable starting point for most terminal windows is width=60, height=20. For detailed inspection of a narrow feature (like a sharp peak), zoom in by tightening x_min and x_max and increasing width.

Handling Non-Finite Points

plot_ascii evaluates the expression for each x sample and silently skips any point that produces a non-finite result — that is, f64::INFINITY, f64::NEG_INFINITY, or NaN. This means functions with poles or discontinuities plot cleanly without panicking:
use mathcore::MathCore;

// tan(x) has poles at ±π/2 — those points are simply skipped
let plot = MathCore::plot_ascii("tan(x)", "x", -1.4, 1.4, 60, 20).unwrap();
println!("{}", plot);
If every evaluated point in the range is non-finite, plot_ascii returns Err(MathError::InvalidOperation("No valid points to plot")). Make sure your x range contains at least one finite point for the expression.

Build docs developers (and LLMs) love