calc

package module
v0.0.0-...-930beaa Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: BSD-3-Clause Imports: 9 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

calc

A command line calculator.

Installation

git clone https://github.com/pointlander/calc.git
cd calc
go install

Usage

calc

Language

e <- sp e1 !.
e1 <- e2 ( add e2
         / minus e2
         )*
e2 <- e3 ( multiply e3
         / divide e3
         / modulus e3
         )*
e3 <- e4 ( exponentiation e4
         )*
e4 <- minus value
    / value
value <- matrix
       / imaginary
       / number
       / exp1
       / exp2
       / natural
       / pi
       / prec
       / simplify
       / derivative
       / log
       / sqrt
       / cos
       / sin
       / tan
       / variable
       / sub
variable <- [A-Za-z]+ sp
matrix <- '[' sp (e1 / row)+ ']' sp
imaginary <- < [-]? [0-9]+ ([.] [0-9]*)? > 'i' sp
number <- < [-]? [0-9]+ ([.] [0-9]*)? > sp
exp1 <- 'exp' open e1 close
exp2 <- 'e^' value
natural <- 'e' sp
pi <- 'pi' sp
prec <- 'prec' open e1 close
simplify <- 'simplify' open e1 close
derivative <- 'derivative' open e1 close
log <- 'log' open e1 close
sqrt <- 'sqrt' open e1 close
cos <- 'cos' open e1 close
sin <- 'sin' open e1 close
tan <- 'tan' open e1 close
sub <- open e1 close
add <- '+' sp
minus <- '-' sp
multiply <- '*' sp
divide <- '/' sp
modulus <- '%' sp
exponentiation <- '^' sp
open <- '(' sp
close <- ')' sp
sp <- ( ' ' / '\t' )*
row <- ';' sp

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisableMemoize

func DisableMemoize[U Uint]() func(*Calculator[U]) error

func Pretty

func Pretty[U Uint](pretty bool) func(*Calculator[U]) error

func Size

func Size[U Uint](size int) func(*Calculator[U]) error

Types

type Calculator

type Calculator[U Uint] struct {
	Buffer string

	Pretty bool
	// contains filtered or unexported fields
}

func (*Calculator) AST

func (t *Calculator) AST() *node[U]

func (*Calculator) Add

func (t *Calculator) Add(rule pegRule, begin, end, index U)

func (*Calculator[T]) Convert

func (c *Calculator[T]) Convert(n *node[T]) Value

Convert converts to an expression

func (*Calculator[T]) Eval

func (c *Calculator[T]) Eval() Value

Eval evaluates the expression

func (*Calculator[U]) Init

func (p *Calculator[U]) Init(options ...func(*Calculator[U]) error) error

func (*Calculator[_]) Parse

func (p *Calculator[_]) Parse(rule ...int) error

func (*Calculator) PrettyPrintSyntaxTree

func (t *Calculator) PrettyPrintSyntaxTree(buffer string)

func (*Calculator) Print

func (t *Calculator) Print()

func (*Calculator[_]) PrintSyntaxTree

func (p *Calculator[_]) PrintSyntaxTree()

func (*Calculator[_]) Reset

func (p *Calculator[_]) Reset()

func (*Calculator[_]) SprintSyntaxTree

func (p *Calculator[_]) SprintSyntaxTree() string

func (*Calculator) Tokens

func (t *Calculator) Tokens() []token[U]

func (*Calculator) Trim

func (t *Calculator) Trim(length uint32)

func (*Calculator[_]) WriteSyntaxTree

func (p *Calculator[_]) WriteSyntaxTree(w io.Writer)

type Float

type Float struct {
	A *big.Float // Real part
	B *big.Float // Imaginary part
}

Float is an imaginary number

func NewFloat

func NewFloat(a, b *big.Float) *Float

NewFloat creates a new imaginary number

func (*Float) Abs

func (f *Float) Abs(a *Float) *Float

Abs computes the absolute value of a

func (*Float) Add

func (f *Float) Add(a, b *Float) *Float

Add add two imaginary numbers

func (*Float) Arg

func (f *Float) Arg(x *Float) *Float

Arg computes arg(x + yi) using atan2(y, x) in (−π, π]. https://mathworld.wolfram.com/ComplexArgument.html

func (*Float) Atan2

func (f *Float) Atan2(x *Float) *Float

Atan2 computes atan2(y, x) for the complex value x+yi (same as Arg). https://en.wikipedia.org/wiki/Atan2

func (*Float) Conj

func (f *Float) Conj(a *Float) *Float

Conj computes the complex conjugate of a

func (*Float) Cos

func (f *Float) Cos(x *Float) *Float

Cos computes cosine of a number https://www.wolframalpha.com/input/?i=cos%28x+%2B+yi%29

func (*Float) Div

func (f *Float) Div(a, b *Float) *Float

Div divides two imaginary numbers: (a+bi)/(c+di) = ((ac+bd)+(bc-ad)i)/(c²+d²).

func (*Float) Exp

func (f *Float) Exp(x *Float) *Float

Exp computes e^x for a complex number https://www.wolframalpha.com/input/?i=e%5E%28x+%2B+yi%29

func (*Float) Log

func (f *Float) Log(x *Float) *Float

Log computes the natural log of x: log|z| + i·arg(z) https://en.wikipedia.org/wiki/Complex_logarithm

func (*Float) Mul

func (f *Float) Mul(a, b *Float) *Float

Mul multiples two imaginary numbers

func (*Float) Pow

func (f *Float) Pow(x *Float, y *Float) *Float

Pow computes x**y https://mathworld.wolfram.com/ComplexExponentiation.html

func (*Float) Rat

func (f *Float) Rat(r *Rational)

Rat stores the float in a rational

func (*Float) SetRat

func (f *Float) SetRat(r *Rational)

SetRat sets the value to a rational

func (*Float) Sin

func (f *Float) Sin(x *Float) *Float

Sin computes sine of a number https://www.wolframalpha.com/input/?i=sin%28x+%2B+yi%29

func (*Float) Sqrt

func (f *Float) Sqrt(a *Float) *Float

Sqrt computes the square root of the complex number https://www.johndcook.com/blog/2020/06/09/complex-square-root/

func (*Float) String

func (f *Float) String() string

String returns a string of the complex number.

func (*Float) Sub

func (f *Float) Sub(a, b *Float) *Float

Sub subtracts two imaginary numbers

func (*Float) Tan

func (f *Float) Tan(x *Float) *Float

Tan computes tangent of a number https://en.wikipedia.org/wiki/Trigonometric_functions

type Matrix

type Matrix struct {
	Prec   uint
	Values [][]Rational
}

Matrix is a matrix

func NewMatrix

func NewMatrix(prec uint) Matrix

NewMatrix make a new matrix

func (*Matrix) Abs

func (m *Matrix) Abs(a *Matrix) *Matrix

Abs computes the absolute value of the entries of the matrix

func (*Matrix) Add

func (m *Matrix) Add(a, b *Matrix) *Matrix

Add adds two matricies

func (*Matrix) Arg

func (m *Matrix) Arg(a *Matrix) *Matrix

Arg computes arg(x + yi) = tan-1(y/x) https://mathworld.wolfram.com/ComplexArgument.html

func (*Matrix) Atan2

func (m *Matrix) Atan2(a *Matrix) *Matrix

Atan2 computes atan2 of x https://en.wikipedia.org/wiki/Atan2

func (*Matrix) Conj

func (m *Matrix) Conj(a *Matrix) *Matrix

Conj computes the complex conjugate of a

func (*Matrix) Cos

func (m *Matrix) Cos(a *Matrix) *Matrix

Cos computes cosine of a number https://www.wolframalpha.com/input/?i=cos%28x+%2B+yi%29

func (*Matrix) Div

func (m *Matrix) Div(a, b *Matrix) *Matrix

Div divides two matricies

func (*Matrix) Exp

func (m *Matrix) Exp(a *Matrix) *Matrix

Exp computes e^x for a complex number https://www.wolframalpha.com/input/?i=e%5E%28x+%2B+yi%29

func (*Matrix) Im

func (m *Matrix) Im(a *Matrix) *Matrix

Im extracts the imaginary part of each entry (as a real number)

func (*Matrix) Log

func (m *Matrix) Log(a *Matrix) *Matrix

Log computes the natural log of x https://en.wikipedia.org/wiki/Complex_logarithm

func (*Matrix) Mul

func (m *Matrix) Mul(a, b *Matrix) *Matrix

Mul multiplies two matricies

func (*Matrix) Neg

func (m *Matrix) Neg(a *Matrix) *Matrix

Neg negates the rational

func (*Matrix) Pow

func (m *Matrix) Pow(x *Matrix, y *Rational) *Matrix

Pow computes x**y https://mathworld.wolfram.com/ComplexExponentiation.html

func (*Matrix) Re

func (m *Matrix) Re(a *Matrix) *Matrix

Re extracts the real part of each entry

func (*Matrix) Sin

func (m *Matrix) Sin(a *Matrix) *Matrix

Sin computes sine of a number https://www.wolframalpha.com/input/?i=sin%28x+%2B+yi%29

func (*Matrix) Sqrt

func (m *Matrix) Sqrt(a *Matrix) *Matrix

Sqrt computes the square root of the matrix

func (*Matrix) String

func (m *Matrix) String() string

func (*Matrix) Sub

func (m *Matrix) Sub(a, b *Matrix) *Matrix

Sub subtracts two matricies

func (*Matrix) Tan

func (m *Matrix) Tan(a *Matrix) *Matrix

Tan computes tangent of a number https://en.wikipedia.org/wiki/Trigonometric_functions

type Node

type Node struct {
	Operation   Operation
	Value       string
	Matrix      *Matrix
	Left, Right *Node
}

Node is a node in an expression binary tree

func (*Node) Equals

func (n *Node) Equals(x int64) bool

Equals test if value is equal to x

func (*Node) Eval

func (n *Node) Eval() (*Node, *Matrix)

Eval evaluates an expression

func (*Node) Integrate

func (n *Node) Integrate() *Node

Integrate integrates the expression symbolically (indefinite integral). The constant of integration is omitted. Assumes a single free variable. Products of non-constants are handled with integration by parts (LIATE), including cyclic cases such as ∫ e^x sin(x) dx.

func (*Node) Simplify

func (n *Node) Simplify() *Node

Simplify simplifies an expression. Repeated passes fold constants, cancel factors, remove double negations, and factor common terms.

func (*Node) String

func (n *Node) String() string

String returns the string form of the equation

type Operation

type Operation uint

Operation is a mathematical operation

const (
	// OperationNoop is a noop
	OperationNoop Operation = iota
	// OperationAdd adds two numbers
	OperationAdd
	// OperationSubtract subtracts two numbers
	OperationSubtract
	// OperationMultiply multiplies two numbers
	OperationMultiply
	// OperationDivide divides two numbers
	OperationDivide
	// OperationModulus computes the modulus of two numbers
	OperationModulus
	// OperationExponentiation raises a number to a number
	OperationExponentiation
	// OperationNegate changes the sign of a number
	OperationNegate
	// OperationVariable is a variable
	OperationVariable
	// OperationImaginary is an imaginary number
	OperationImaginary
	// OperationNumber is a real number
	OperationNumber
	// OperationMatrix is a matrix
	OperationMatrix
	// OperationNaturalExponentiation raises the natural number to a power
	OperationNaturalExponentiation
	// OperationNatural is the constant e
	OperationNatural
	// OperationPI is the constant pi
	OperationPI
	// OperationSetPrec sets the precision
	OperationSetPrec
	// OperationSimplify simplify the expression
	OperationSimplify
	// OperationDerivative compute the symbolic derivative
	OperationDerivative
	// OperationIntegrate integrates the equation
	OperationIntegrate
	// OperationNaturalLogarithm os the natural logarithm
	OperationNaturalLogarithm
	// OperationSquareRoot computes the square root of a number
	OperationSquareRoot
	// OperationCosine computes the cosine of a number
	OperationCosine
	// OperationSine computes the sine of a number
	OperationSine
	// OperationTangent computes the tangent of a number
	OperationTangent
	// OperationAbs computes the absolute value / modulus
	OperationAbs
	// OperationConj computes the complex conjugate
	OperationConj
	// OperationRe extracts the real part
	OperationRe
	// OperationIm extracts the imaginary part
	OperationIm
	// OperationNotation is E notation operation
	OperationNotation
)

type Rational

type Rational struct {
	A *big.Rat // Real part
	B *big.Rat // Imaginary part
}

Rational is an imaginary number

func NewRational

func NewRational(a, b *big.Rat) *Rational

NewRational creates a new imaginary number

func (*Rational) Add

func (r *Rational) Add(a, b *Rational) *Rational

Add add two imaginary numbers

func (*Rational) Conj

func (r *Rational) Conj(a *Rational) *Rational

Conj computes the complex conjugate of a

func (*Rational) Div

func (r *Rational) Div(a, b *Rational) *Rational

Div divides two imaginary numbers: (a+bi)/(c+di) = ((ac+bd)+(bc-ad)i)/(c²+d²). Copies inputs so callers may pass overlapping or shared rationals safely.

func (*Rational) Mul

func (r *Rational) Mul(a, b *Rational) *Rational

Mul multiples two imaginary numbers

func (*Rational) Neg

func (r *Rational) Neg(a *Rational) *Rational

Neg negates the rational

func (*Rational) String

func (r *Rational) String() string

String returns a string of the complex rational.

func (*Rational) Sub

func (r *Rational) Sub(a, b *Rational) *Rational

Sub subtracts two imaginary numbers

type Uint

type Uint interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

type Value

type Value struct {
	ValueType  ValueType
	Matrix     *Matrix
	Expression *Node
}

Value is a value

type ValueType

type ValueType int

ValueType is a value type

const (
	// ValueTypeMatrix is a matrix value type
	ValueTypeMatrix ValueType = iota
	// ValueTypeExpression is an expression value type
	ValueTypeExpression
)

Directories

Path Synopsis
cmd
calc command

Jump to

Keyboard shortcuts

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