nerdamer

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2025 License: BSD-3-Clause Imports: 8 Imported by: 0

README

Go Reference LiberaPay receives patrons

logo_png

Package nerdamer provides a small and light-weight symbolic math expression evaluator.

The back end used is Nerdamer-prime.

Documentation

Overview

Package nerdamer provides a small and light-weight symbolic math expression evaluator.

The back end used is Nerdamer-prime.

Example (Diff)

Evaluating diff via nerdamer(string).

n, _ := New()
defer n.Close()
fmt.Println(n.Eval("nerdamer('diff(sin(x)/x, x)').toString()"))
Output:

-sin(x)*x^(-2)+cos(x)*x^(-1) <nil>
Example (Diff2)

Evaluating diff via the property of the nerdamer object.

n, _ := New()
defer n.Close()
fmt.Println(n.Eval("nerdamer.diff('sin(x)/x', 'x').toString()"))
Output:

-sin(x)*x^(-2)+cos(x)*x^(-1) <nil>
Example (Expand)

Evaluating expand as a string.

n, _ := New()
defer n.Close()
fmt.Println(n.Eval("nerdamer('expand((x+1)^2)').toString()"))
Output:

1+2*x+x^2 <nil>
Example (Expand2)

Evaluating expand as JSON for machine processing.

n, _ := New()
defer n.Close()
fmt.Println(n.Eval("nerdamer('expand((x+1)^2)')"))
Output:

{"symbol":{"group":8,"value":"1+2*x+x^2","multiplier":{"num":"1","den":"1"},"power":{"num":"1","den":"1"},"symbols":{"x":{"group":6,"value":"x","multiplier":{"num":"1","den":"1"},"power":{"num":"1","den":"1"},"symbols":{"1":{"group":3,"value":"x","multiplier":{"num":"2","den":"1"},"power":{"num":"1","den":"1"},"imaginary":false,"isInfinity":false},"2":{"group":3,"value":"x","multiplier":{"num":"1","den":"1"},"power":{"num":"2","den":"1"},"imaginary":false,"isInfinity":false}},"length":2,"imaginary":false,"isInfinity":false},"#":{"group":1,"value":"#","multiplier":{"num":"1","den":"1"},"power":{"num":"1","den":"1"}}},"length":2,"imaginary":false,"isInfinity":false}} <nil>
Example (Expand3)

Evaluating expand as formatted JSON for a human reader.

n, _ := New()
defer n.Close()
fmt.Println(n.Eval("JSON.stringify(nerdamer('expand((x+1)^2)'), null, 2)"))
Output:

{
  "symbol": {
    "group": 8,
    "value": "1+2*x+x^2",
    "multiplier": {
      "num": "1",
      "den": "1"
    },
    "power": {
      "num": "1",
      "den": "1"
    },
    "symbols": {
      "x": {
        "group": 6,
        "value": "x",
        "multiplier": {
          "num": "1",
          "den": "1"
        },
        "power": {
          "num": "1",
          "den": "1"
        },
        "symbols": {
          "1": {
            "group": 3,
            "value": "x",
            "multiplier": {
              "num": "2",
              "den": "1"
            },
            "power": {
              "num": "1",
              "den": "1"
            },
            "imaginary": false,
            "isInfinity": false
          },
          "2": {
            "group": 3,
            "value": "x",
            "multiplier": {
              "num": "1",
              "den": "1"
            },
            "power": {
              "num": "2",
              "den": "1"
            },
            "imaginary": false,
            "isInfinity": false
          }
        },
        "length": 2,
        "imaginary": false,
        "isInfinity": false
      },
      "#": {
        "group": 1,
        "value": "#",
        "multiplier": {
          "num": "1",
          "den": "1"
        },
        "power": {
          "num": "1",
          "den": "1"
        }
      }
    },
    "length": 2,
    "imaginary": false,
    "isInfinity": false
  }
} <nil>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Nerdamer

type Nerdamer struct {
	// contains filtered or unexported fields
}

Nerdamer is the evaluator instance.

func New

func New() (r *Nerdamer, err error)

New returns a newly created Nerdamer. The instance must be eventually properly Nerdamer.Close-ed to prevent resource leaks.

func (*Nerdamer) Close

func (n *Nerdamer) Close() (err error)

Close cleanups 'n' and releases its resources.

func (*Nerdamer) Eval

func (n *Nerdamer) Eval(js string) (r any, err error)

Eval returns the result of evaluating 'js'.

Additional info migth be available at nerdamer and/or nerdamer-prime.

func (*Nerdamer) EvalValue added in v0.1.0

func (n *Nerdamer) EvalValue(js string) (r quickjs.Value, err error)

EvalValue is like Eval but returns quickjs.Value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL