Sha256: fbee8673f055b61bab2ec3a31b5c3e517fca4d7b35a908e6cf3542ab8f7896da

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

require 'spec_helper'

describe Dydx::Function do
  before { reset }

  it 'ex1' do
    expect(f(x, y)).to eq(f(x, y))
    expect(f(x, y)).to eq($f)
    expect(f(x, y).algebra).to be_nil
    expect(f(x, y).vars).to eq([:x, :y])
    expect { f(x, y, z) }.to raise_error(ArgumentError)
    expect { f(x) }.to raise_error(ArgumentError)
  end

  it 'ex2' do
    expect(f(x, y) <= x * y ).to eq(f(x, y))
    expect(f(x, y)).to eq(f(x, y))
    expect(f(x, y)).to eq($f)
    expect(f(x, y).algebra).to eq(x * y)
    expect(f(x, y)).to eq(x * y)
    expect(f(x, y)).to eq(eval(f(x, y).to_s))
    expect(f(a, b)).to eq(a * b)
    expect(f(2, x)).to eq(2 * x)
    expect(f(2, 3)).to eq(6)
    expect(f(a + b, c)).to eq((a + b) * c)
    expect(d/dx(f(x, y))).to eq(y)
    expect(d/dy(f(x, y))).to eq(x)
    expect(d/dz(f(x, y))).to eq(0)
  end

  it 'ex3' do
    f(x, y) <= x * y
    g(a, b) <= f(a + b, b)
    expect(g(a, b)).to eq((a + b) * b)
    expect(g(a, 3)).to eq(9 + 3 * a)
    expect(g(2, 3)).to eq(15)
  end

  it 'ex4' do
    f(a, b) <= (a + b) * b
    h(a, b, c) <= d/db(f(a, b))
    expect(h(a, b, c)).to eq(( a + ( 2 * b ) ))
    expect(h(a, b, c).algebra).to eq(( a + ( 2 * b ) ))
  end

  it 'ex5' do
    f(x) <= log(x)
    expect(f(e)).to eq(1)
    expect(f(0)).to eq(-oo)
    expect(f(y)).to eq(log(y))

    g(x) <= d/dx(f(x))
    expect(g(1)).to eq(1)
  end

  it 'ex6' do
    f(x) <= sin(x)
    expect(f(pi)).to eq(0)
    expect(f(pi / 2)).to eq(1)
    expect(f(y)).to eq(sin(y))

    g(x) <= d/dx(f(x))
    expect(g(pi)).to eq(-1)
  end

  it 'ex7' do
    f(x) <= (1 + 1/x) ** x
    expect(f(100000)).to eq(2.7182682371744895)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dydx-0.2.7000001 spec/lib/function_spec.rb
dydx-0.2.7000000 spec/lib/function_spec.rb