SkillAgentSearch skills...

Dual

Go implementation of dual numbers.

Install / Use

/learn @tamnd/Dual
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Dual

The dual package defines the Dual type to represent dual numbers. For simple explaination, see the blog post by Simon.

In linear algebra, the dual numbers extend the real numbers by adjoining one new element ε with the property ε^2 = 0 (ε is nilpotent), in a similar way that complex numbers adjoin the imaginary unit i with the property i*i=-1.

Install

go get github.com/tamnd/dual

Usage

Use the dual.New() to define the dual number 2 + 1*ɛ:

x := dual.New(2, 1)

Define a function f(x) = x^2/(x+1) as:

func f(x dual.Dual) dual.Dual {
   return dual.Div(dual.Mul(x, x), dual.Add(x, dual.FromFloat(1.0))
}

If we want to find the first derivative of f(x) at x=2, i.e, f'(2), we could find it by using dual number arithmetic:

y := f(x)
fmt.Println("f(x) = x^2/(x+1)")
fmt.Println("f(2) = ", dual.Real(y))
fmt.Println("f'(2) = ", dual.Epsilon(y))

The complete example:

package main

import "github.com/tamnd/dual"

func f(x dual.Dual) dual.Dual {
   return dual.Div(dual.Mul(x, x), dual.Add(x, dual.FromFloat(1.0))
}

func main() {
    x := dual.New(2, 1)
    y := f(x)
    fmt.Println("f(x) = x^2/(x+1)")
    fmt.Println("f(2) = ", dual.Real(y))
    fmt.Println("f'(2) = ", dual.Epsilon(y))
}
View on GitHub
GitHub Stars8
CategoryDevelopment
Updated7y ago
Forks1

Languages

Go

Security Score

70/100

Audited on Dec 10, 2018

No findings