source stringclasses 1
value | repo stringlengths 5 63 | repo_url stringlengths 24 82 | path stringlengths 5 167 | language stringclasses 1
value | license stringclasses 5
values | stars int64 10 51.4k | ref stringclasses 23
values | size_bytes int64 200 258k | text stringlengths 137 258k |
|---|---|---|---|---|---|---|---|---|---|
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/unary_inverse.rb | Ruby | mit | 87 | master | 956 | module Keisan
module AST
class UnaryInverse < UnaryOperator
def value(context = nil)
return Rational(1, child.value(context))
end
def to_s
"(#{child.to_s})**(-1)"
end
def evaluate(context = nil)
1.to_node / child.evaluate(context)
end
def simpli... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/logical_operator.rb | Ruby | mit | 87 | master | 742 | module Keisan
module AST
class LogicalOperator < Operator
def evaluate(context = nil)
context ||= Context.new
children[0].evaluate(context).send(operator, children[1].evaluate(context))
end
def simplify(context = nil)
context ||= Context.new
children[0].simplify(... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/function_assignment.rb | Ruby | mit | 87 | master | 2,024 | module Keisan
module AST
class FunctionAssignment
attr_reader :context, :lhs, :rhs, :local
def initialize(context, lhs, rhs, local)
@context = context
@lhs = lhs
@rhs = rhs
@local = local
unless lhs.children.all? {|arg| arg.is_a?(Variable)}
raise Exc... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/line_builder.rb | Ruby | mit | 87 | master | 8,542 | module Keisan
module AST
class LineBuilder
# Build from parser
def initialize(components)
@components = components
@nodes = components_to_basic_nodes(@components)
# Negative means not an operator
@priorities = @nodes.map {|node| node.is_a?(Keisan::Parsing::Operator) ? ... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/date_time_methods.rb | Ruby | mit | 87 | master | 1,493 | module Keisan
module AST
module DateTimeMethods
def +(other)
other = other.to_node
case other
when Number
self.class.new(value + other.value)
else
super
end
end
def >(other)
other = other.to_node
case other
when... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/logical_not_equal.rb | Ruby | mit | 87 | master | 249 | module Keisan
module AST
class LogicalNotEqual < LogicalOperator
def self.symbol
:"!="
end
protected
def value_operator
:!=
end
def operator
:not_equal
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/block.rb | Ruby | mit | 87 | master | 1,296 | module Keisan
module AST
class Block < Node
attr_reader :child
def initialize(child)
@child = child
end
def unbound_variables(context = nil)
local = get_local_context(context)
child.unbound_variables(local)
end
def unbound_functions(context = nil)
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/logical_greater_than_or_equal_to.rb | Ruby | mit | 87 | master | 254 | module Keisan
module AST
class LogicalGreaterThanOrEqualTo < LogicalOperator
def self.symbol
:">="
end
protected
def value_operator
:>=
end
def operator
:>=
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/logical_equal.rb | Ruby | mit | 87 | master | 242 | module Keisan
module AST
class LogicalEqual < LogicalOperator
def self.symbol
:"=="
end
protected
def value_operator
:==
end
def operator
:equal
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/logical_or.rb | Ruby | mit | 87 | master | 854 | module Keisan
module AST
class LogicalOr < LogicalOperator
def self.symbol
:"||"
end
def blank_value
false
end
def evaluate(context = nil)
short_circuit_do(:evaluate, context)
end
def simplify(context = nil)
short_circuit_do(:simplify, c... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/logical_and.rb | Ruby | mit | 87 | master | 857 | module Keisan
module AST
class LogicalAnd < LogicalOperator
def self.symbol
:"&&"
end
def blank_value
true
end
def evaluate(context = nil)
short_circuit_do(:evaluate, context)
end
def simplify(context = nil)
short_circuit_do(:simplify, c... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/logical_greater_than.rb | Ruby | mit | 87 | master | 242 | module Keisan
module AST
class LogicalGreaterThan < LogicalOperator
def self.symbol
:">"
end
protected
def value_operator
:>
end
def operator
:>
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/string.rb | Ruby | mit | 87 | master | 888 | module Keisan
module AST
class String < ConstantLiteral
attr_reader :content
def initialize(content)
@content = content
end
def value(context = nil)
content
end
def +(other)
case other
when String
String.new(value + other.value)
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/list_assignment.rb | Ruby | mit | 87 | master | 1,103 | module Keisan
module AST
class ListAssignment
attr_reader :assignment, :context, :lhs, :rhs
def initialize(assignment, context, lhs, rhs)
@assignment = assignment
@context = context
@lhs = lhs
@rhs = rhs
end
def evaluate
rhs = @rhs.evaluate(context... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/unary_plus.rb | Ruby | mit | 87 | master | 399 | module Keisan
module AST
class UnaryPlus < UnaryIdentity
def value(context = nil)
return children.first.value(context)
end
def self.symbol
:"+"
end
def simplify(context = nil)
case child
when Number
Number.new(child.value(context)).simplify... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/logical_less_than_or_equal_to.rb | Ruby | mit | 87 | master | 251 | module Keisan
module AST
class LogicalLessThanOrEqualTo < LogicalOperator
def self.symbol
:"<="
end
protected
def value_operator
:<=
end
def operator
:<=
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/assignment.rb | Ruby | mit | 87 | master | 3,157 | require_relative "variable_assignment"
require_relative "function_assignment"
require_relative "list_assignment"
require_relative "cell_assignment"
module Keisan
module AST
class Assignment < Operator
attr_reader :local, :compound_operator
def initialize(children = [], parsing_operators = [], local:... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/operator.rb | Ruby | mit | 87 | master | 3,634 | module Keisan
module AST
class Operator < Parent
# NOTE: operators with same priority must have same associativity
ARITY_PRIORITY_ASSOCIATIVITY = {
"u!": [1, 100, :right], # Logical not
"u~": [1, 100, :right], # Bitwise not
"u+": [1, 100, :right], # Unary plus
"**": ... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/modulo.rb | Ruby | mit | 87 | master | 460 | module Keisan
module AST
class Modulo < ArithmeticOperator
def self.symbol
:%
end
def evaluate(context = nil)
children[1..-1].inject(children.first.evaluate(context)) {|total, child| total % child.evaluate(context)}
end
def value(context = nil)
children_valu... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/date.rb | Ruby | mit | 87 | master | 330 | require_relative "date_time_methods"
module Keisan
module AST
class Date < ConstantLiteral
include DateTimeMethods
attr_reader :date
def initialize(date)
@date = date
end
def value(context = nil)
date
end
def to_s
value.to_s
end
end
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/indexing.rb | Ruby | mit | 87 | master | 2,040 | module Keisan
module AST
class Indexing < UnaryOperator
attr_reader :indexes
def initialize(child, indexes = [])
@children = [child]
@indexes = indexes
end
def deep_dup
dupped = super
dupped.instance_variable_set(
:@indexes, indexes.map(&:deep_du... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/builder.rb | Ruby | mit | 87 | master | 1,004 | module Keisan
module AST
class Builder
# Build from parser
def initialize(string: nil, parser: nil, components: nil)
if [string, parser, components].select(&:nil?).size != 2
raise Exceptions::InternalError.new("Require one of string, parser or components")
end
if !st... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/multi_line.rb | Ruby | mit | 87 | master | 2,096 | module Keisan
module AST
class MultiLine < Parent
def value(context = nil)
context ||= Context.new
evaluate(context).value(context)
end
def evaluate_assignments(context = nil)
self
end
def evaluate(context = nil)
context ||= Context.new
@chil... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/times.rb | Ruby | mit | 87 | master | 2,014 | module Keisan
module AST
class Times < ArithmeticOperator
def initialize(children = [], parsing_operators = [])
super
convert_divide_to_inverse!(parsing_operators)
end
def self.symbol
:*
end
def blank_value
1
end
def evaluate(context = n... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/time.rb | Ruby | mit | 87 | master | 355 | require_relative "date_time_methods"
module Keisan
module AST
class Time < ConstantLiteral
include DateTimeMethods
attr_reader :time
def initialize(time)
@time = time
end
def value(context = nil)
time
end
def to_s
value.strftime("%Y-%m-%d %H:%... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/plus.rb | Ruby | mit | 87 | master | 3,064 | module Keisan
module AST
class Plus < ArithmeticOperator
def initialize(children = [], parsing_operators = [])
super
convert_minus_to_plus!(parsing_operators)
end
def self.symbol
:+
end
def blank_value
0
end
def value(context = nil)
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/unary_bitwise_not.rb | Ruby | mit | 87 | master | 289 | module Keisan
module AST
class UnaryBitwiseNot < UnaryOperator
def value(context = nil)
return ~child.value(context)
end
def evaluate(context = nil)
~child.evaluate(context)
end
def self.symbol
:"~"
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/logical_less_than.rb | Ruby | mit | 87 | master | 239 | module Keisan
module AST
class LogicalLessThan < LogicalOperator
def self.symbol
:"<"
end
protected
def value_operator
:<
end
def operator
:<
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/bitwise_left_shift.rb | Ruby | mit | 87 | master | 337 | module Keisan
module AST
class BitwiseLeftShift < BitwiseOperator
def self.symbol
:<<
end
def blank_value
0
end
def evaluate(context = nil)
children[1..-1].inject(children.first.evaluate(context)) {|total, child| total << child.evaluate(context)}
end
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/number.rb | Ruby | mit | 87 | master | 3,950 | module Keisan
module AST
class Number < ConstantLiteral
attr_reader :number
def initialize(number)
@number = number
# Reduce the number if possible
case @number
when Rational
@number = @number.numerator if @number.denominator == 1
end
end
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/ast/bitwise_or.rb | Ruby | mit | 87 | master | 330 | module Keisan
module AST
class BitwiseOr < BitwiseOperator
def self.symbol
:"|"
end
def blank_value
0
end
def evaluate(context = nil)
children[1..-1].inject(children.first.evaluate(context)) {|total, child| total | child.evaluate(context)}
end
end
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/erf.rb | Ruby | mit | 87 | master | 269 | module Keisan
module Functions
class Erf < MathFunction
def initialize
super("erf")
end
protected
def self.derivative(argument)
2/Math.sqrt(Math::PI) * AST::Function.new([-argument**2], "exp")
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/default_registry.rb | Ruby | mit | 87 | master | 6,041 | require_relative "let"
require_relative "puts"
require_relative "break"
require_relative "continue"
require_relative "if"
require_relative "while"
require_relative "diff"
require_relative "replace"
require_relative "range"
require_relative "map"
require_relative "filter"
require_relative "reduce"
require_relative "to_... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/log.rb | Ruby | mit | 87 | master | 218 | module Keisan
module Functions
class Log < CMathFunction
def initialize
super("log")
end
protected
def self.derivative(argument)
1 / argument
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/enumerable_function.rb | Ruby | mit | 87 | master | 2,039 | module Keisan
module Functions
class EnumerableFunction < Function
# Filters lists/hashes:
# (list, variable, boolean_expression)
# (hash, key, value, boolean_expression)
def initialize(name)
super(name, -3)
end
def value(ast_function, context = nil)
evaluate(a... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/sample.rb | Ruby | mit | 87 | master | 573 | module Keisan
module Functions
class Sample < ProcFunction
def initialize
@name = "sample"
@arity = ::Range.new(1, 2)
end
# Single argument: list to sample element from
# Double argument: list and number of elements to sample
def call(context, *args)
case arg... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/sec.rb | Ruby | mit | 87 | master | 344 | module Keisan
module Functions
class Sec < CMathFunction
def initialize
super("sec", Proc.new {|arg| 1 / CMath::cos(arg)})
end
protected
def self.derivative(argument)
AST::Function.new([argument], "sin") * AST::Exponent.new([AST::Function.new([argument], "cos"), -2])
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/range.rb | Ruby | mit | 87 | master | 2,047 | module Keisan
module Functions
class Range < Function
def initialize
super("range", ::Range.new(1,3))
end
def call(context, *args)
start, finish, shift = start_finish_shift_from_args(*args)
if shift == 1
start_finish_range(start, finish)
else
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/expression_function.rb | Ruby | mit | 87 | master | 3,851 | module Keisan
module Functions
class ExpressionFunction < Function
attr_reader :arguments, :expression
def initialize(name, arguments, expression, transient_definitions)
super(name, arguments.count)
if expression.is_a?(::String)
@expression = AST::parse(expression)
e... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/replace.rb | Ruby | mit | 87 | master | 1,462 | module Keisan
module Functions
class Replace < Function
def initialize
@name = "replace"
end
def value(ast_function, context = nil)
context ||= Context.new
evaluate(ast_function, context).value(context)
end
def simplify(ast_function, context = nil)
c... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/map.rb | Ruby | mit | 87 | master | 1,513 | require "keisan/functions/enumerable_function"
module Keisan
module Functions
class Map < EnumerableFunction
# Maps
# (list, variable, expression)
# (hash, key, value, expression)
def initialize
super("map")
end
protected
def shadowing_variable_names(children)
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/break.rb | Ruby | mit | 87 | master | 217 | require_relative "loop_control_flow_function"
module Keisan
module Functions
class Break < LoopControlFlowFuntion
def initialize
super("break", Exceptions::BreakError)
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/to_h.rb | Ruby | mit | 87 | master | 847 | module Keisan
module Functions
class ToH < Function
def initialize
super("to_h", 1)
end
def value(ast_function, context = nil)
validate_arguments!(ast_function.children.count)
evaluate(ast_function, context).value(context)
end
def evaluate(ast_function, cont... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/sinh.rb | Ruby | mit | 87 | master | 245 | module Keisan
module Functions
class Sinh < CMathFunction
def initialize
super("sinh")
end
protected
def self.derivative(argument)
AST::Function.new([argument], "cosh")
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/coth.rb | Ruby | mit | 87 | master | 310 | module Keisan
module Functions
class Coth < CMathFunction
def initialize
super("coth", Proc.new {|arg| 1 / CMath::tanh(arg)})
end
protected
def self.derivative(argument)
-AST::Exponent.new([AST::Function.new([argument], "sinh"), -2])
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/while.rb | Ruby | mit | 87 | master | 2,426 | module Keisan
module Functions
class While < Keisan::Function
class WhileLogicalNodeIsNotConstant < Keisan::Exceptions::StandardError; end
class WhileLogicalNodeIsNonBoolConstant < Keisan::Exceptions::StandardError; end
def initialize
super("while", 2)
end
def value(ast_fun... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/continue.rb | Ruby | mit | 87 | master | 226 | require_relative "loop_control_flow_function"
module Keisan
module Functions
class Continue < LoopControlFlowFuntion
def initialize
super("continue", Exceptions::ContinueError)
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/sin.rb | Ruby | mit | 87 | master | 242 | module Keisan
module Functions
class Sin < CMathFunction
def initialize
super("sin")
end
protected
def self.derivative(argument)
AST::Function.new([argument], "cos")
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/loop_control_flow_function.rb | Ruby | mit | 87 | master | 494 | module Keisan
module Functions
class LoopControlFlowFuntion < Function
def initialize(name, exception_class)
super(name, 0)
@exception_class = exception_class
end
def value(ast_function, context = nil)
raise @exception_class.new
end
def evaluate(ast_function... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/cosh.rb | Ruby | mit | 87 | master | 245 | module Keisan
module Functions
class Cosh < CMathFunction
def initialize
super("cosh")
end
protected
def self.derivative(argument)
AST::Function.new([argument], "sinh")
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/math_function.rb | Ruby | mit | 87 | master | 1,014 | module Keisan
module Functions
class MathFunction < ProcFunction
def initialize(name, proc_function = nil)
super(name, proc_function || Proc.new {|arg| Math.send(name, arg)})
end
def simplify(ast_function, context = nil)
simplified = super
self.class.apply_simplification... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/reduce.rb | Ruby | mit | 87 | master | 2,257 | require "keisan/functions/enumerable_function"
module Keisan
module Functions
class Reduce < EnumerableFunction
# Reduces (list, initial, accumulator, variable, expression)
# e.g. reduce([1,2,3,4], 0, total, x, total+x)
# should give 10
# When hash: (hash, initial, accumulator, key, value... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/registry.rb | Ruby | mit | 87 | master | 1,817 | module Keisan
module Functions
class Registry
def initialize(functions: {}, parent: nil, use_defaults: true, force: false)
@hash = {}
@parent = parent
@use_defaults = use_defaults
functions.each do |name, function|
register!(name, function, force: force)
en... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/if.rb | Ruby | mit | 87 | master | 2,031 | module Keisan
module Functions
class If < Function
def initialize
super("if", ::Range.new(2,3))
end
def value(ast_function, context = nil)
validate_arguments!(ast_function.children.count)
evaluate(ast_function, context)
end
def evaluate(ast_function, context... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/let.rb | Ruby | mit | 87 | master | 977 | module Keisan
module Functions
class Let < Function
def initialize
super("let", ::Range.new(1,2))
end
def value(ast_function, context = nil)
evaluate(ast_function, context)
end
def evaluate(ast_function, context = nil)
validate_arguments!(ast_function.childr... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/tan.rb | Ruby | mit | 87 | master | 267 | module Keisan
module Functions
class Tan < CMathFunction
def initialize
super("tan")
end
protected
def self.derivative(argument)
AST::Exponent.new([AST::Function.new([argument], "cos"), -2])
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/sech.rb | Ruby | mit | 87 | master | 350 | module Keisan
module Functions
class Sech < CMathFunction
def initialize
super("sech", Proc.new {|arg| 1 / CMath::cosh(arg)})
end
protected
def self.derivative(argument)
-AST::Function.new([argument], "sinh") * AST::Exponent.new([AST::Function.new([argument], "cosh"), -2]... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/cbrt.rb | Ruby | mit | 87 | master | 257 | module Keisan
module Functions
class Cbrt < CMathFunction
def initialize
super("cbrt")
end
protected
def self.derivative(argument)
AST::Exponent.new([argument, Rational(-2,3)]) / 3
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/tanh.rb | Ruby | mit | 87 | master | 270 | module Keisan
module Functions
class Tanh < CMathFunction
def initialize
super("tanh")
end
protected
def self.derivative(argument)
AST::Exponent.new([AST::Function.new([argument], "cosh"), -2])
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/puts.rb | Ruby | mit | 87 | master | 538 | module Keisan
module Functions
class Puts < Function
def initialize
super("puts", 1)
end
def value(ast_function, context = nil)
evaluate(ast_function, context)
end
def evaluate(ast_function, context = nil)
validate_arguments!(ast_function.children.count)
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/csch.rb | Ruby | mit | 87 | master | 350 | module Keisan
module Functions
class Csch < CMathFunction
def initialize
super("csch", Proc.new {|arg| 1 / CMath::sinh(arg)})
end
protected
def self.derivative(argument)
-AST::Function.new([argument], "cosh") * AST::Exponent.new([AST::Function.new([argument], "sinh"), -2]... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/cos.rb | Ruby | mit | 87 | master | 243 | module Keisan
module Functions
class Cos < CMathFunction
def initialize
super("cos")
end
protected
def self.derivative(argument)
-AST::Function.new([argument], "sin")
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/sqrt.rb | Ruby | mit | 87 | master | 257 | module Keisan
module Functions
class Sqrt < CMathFunction
def initialize
super("sqrt")
end
protected
def self.derivative(argument)
AST::Exponent.new([argument, Rational(-1,2)]) / 2
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/cmath_function.rb | Ruby | mit | 87 | master | 261 | require "cmath"
module Keisan
module Functions
class CMathFunction < MathFunction
def initialize(name, proc_function = nil)
super(name, proc_function || Proc.new {|arg|
CMath.send(name, arg)
})
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/exp.rb | Ruby | mit | 87 | master | 242 | module Keisan
module Functions
class Exp < CMathFunction
def initialize
super("exp")
end
protected
def self.derivative(argument)
AST::Function.new([argument], "exp")
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/rand.rb | Ruby | mit | 87 | master | 542 | module Keisan
module Functions
class Rand < ProcFunction
def initialize
@name = "rand"
@arity = ::Range.new(1,2)
end
# Single argument: integer in range [0, max)
# Double argument: integer in range [min, max)
def call(context, *args)
case args.size
wh... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/csc.rb | Ruby | mit | 87 | master | 345 | module Keisan
module Functions
class Csc < CMathFunction
def initialize
super("csc", Proc.new {|arg| 1 / CMath::sin(arg)})
end
protected
def self.derivative(argument)
-AST::Function.new([argument], "cos") * AST::Exponent.new([AST::Function.new([argument], "sin"), -2])
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/cot.rb | Ruby | mit | 87 | master | 306 | module Keisan
module Functions
class Cot < CMathFunction
def initialize
super("cot", Proc.new {|arg| 1 / CMath::tan(arg)})
end
protected
def self.derivative(argument)
-AST::Exponent.new([AST::Function.new([argument], "sin"), -2])
end
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/proc_function.rb | Ruby | mit | 87 | master | 1,728 | module Keisan
module Functions
class ProcFunction < Function
attr_reader :function_proc
def initialize(name, function_proc)
raise Exceptions::InvalidFunctionError.new unless function_proc.is_a?(Proc)
super(name, function_proc.arity)
@function_proc = function_proc
end
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/filter.rb | Ruby | mit | 87 | master | 2,107 | require "keisan/functions/enumerable_function"
module Keisan
module Functions
class Filter < EnumerableFunction
# Filters lists/hashes:
# (list, variable, boolean_expression)
# (hash, key, value, boolean_expression)
def initialize
super("filter")
end
protected
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | lib/keisan/functions/diff.rb | Ruby | mit | 87 | master | 3,264 | module Keisan
module Functions
class Diff < Function
def initialize
super("diff", -1)
@name = "diff"
end
def value(ast_function, context = nil)
validate_arguments!(ast_function.children.count)
context ||= Context.new
evaluation = evaluate(ast_function, co... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/readme_spec.rb | Ruby | mit | 87 | master | 2,353 | require "spec_helper"
require "digest"
# NOTE: This test uses `eval` on lines pulled from the README.md file, which is dangerous if the
# README ends up containing bad code
#
# To mitigate this problem, we have a hard-coded checksum here that must be updated by hand when
# making code changes. This will force develop... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/spec_helper.rb | Ruby | mit | 87 | master | 618 | require 'simplecov'
SimpleCov.start
require "bundler/setup"
require "keisan"
require "pry"
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
config.expect_with :rspec do |c|
c.syntax = :expect
end
# These tw... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan_spec.rb | Ruby | mit | 87 | master | 1,540 | require "spec_helper"
RSpec.describe Keisan do
context "module methods" do
after do
# Want to reset the calculator internal to Keisan after each spec
# so specs do not interfere
Keisan.reset
end
describe "calculator" do
it "has a calculator for use" do
expect(Keisan.calcu... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/exceptions_spec.rb | Ruby | mit | 87 | master | 748 | require "spec_helper"
RSpec.describe Keisan::Exceptions do
it "has correct structure" do
expect(described_class::BaseError.superclass).to eq ::StandardError
expect(described_class::StandardError.superclass).to eq described_class::BaseError
expect(described_class::InternalError.superclass).to eq describe... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/puts_spec.rb | Ruby | mit | 87 | master | 1,373 | require "spec_helper"
RSpec.describe Keisan::Functions::Puts do
let(:calculator) { Keisan::Calculator.new }
it "outputs to STDOUT" do
expect { calculator.evaluate("puts 123") }.to output("123\n").to_stdout
expect { calculator.evaluate("puts(x**2 + 1)") }.to output("(x**2)+1\n").to_stdout
calculator.ev... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/parser_spec.rb | Ruby | mit | 87 | master | 25,751 | require "spec_helper"
RSpec.describe Keisan::Parser do
it "either string or tokens" do
expect{described_class.new}.to raise_error Keisan::Exceptions::InternalError
end
describe "ast" do
it "returns the AST representation of the string" do
parser = described_class.new(string: "1+x")
ast = par... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/differentiation_spec.rb | Ruby | mit | 87 | master | 1,412 | require "spec_helper"
RSpec.describe "Differentiation" do
let(:calculator) { Keisan::Calculator.new }
describe "diff of expression functions" do
context "single variable" do
it "can be used to assign variables" do
calculator.evaluate("my_func(x) = x*log(x)")
expect(calculator.evaluate("m... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/string_and_group_parser_spec.rb | Ruby | mit | 87 | master | 8,961 | require "spec_helper"
RSpec.describe Keisan::StringAndGroupParser do
describe described_class::OtherPortion do
it "cannot start with a quote or brace character" do
expect { described_class.new("''", 0) }.to raise_error Keisan::Exceptions::TokenizingError
expect { described_class.new("(", 0) }.to rais... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/let_spec.rb | Ruby | mit | 87 | master | 1,833 | require "spec_helper"
RSpec.describe Keisan::Functions::Let do
let(:calculator) { Keisan::Calculator.new }
it "can be used to assign variables" do
calculator.evaluate("let x = 4")
expect(calculator.evaluate("2*x")).to eq 8
end
it "defines variables locally when in block" do
calculator.evaluate("l... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/default_variables_spec.rb | Ruby | mit | 87 | master | 888 | require "spec_helper"
RSpec.describe Keisan::Variables::DefaultRegistry do
let(:registry) { described_class.registry }
it "contains correct variables" do
expect(registry["PI"].value).to eq Math::PI
expect(registry["E"].value).to eq Math::E
expect(registry["I"].value).to eq Complex(0,1)
expect(Kei... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/calculator_spec.rb | Ruby | mit | 87 | master | 15,198 | require "spec_helper"
RSpec.describe Keisan::Calculator do
let(:calculator) { described_class.new }
it "calculates correctly" do
expect(calculator.evaluate("1 + 2")).to eq 3
expect(calculator.evaluate("2*x + 4", x: 3)).to eq 10
expect(calculator.evaluate("2 / 3 ** 2")).to eq Rational(2,9)
end
it ... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/tokenizer_spec.rb | Ruby | mit | 87 | master | 18,948 | require "spec_helper"
RSpec.describe Keisan::Tokenizer do
context "numbers" do
context "integer" do
it "gets integers correctly" do
tokenizer = described_class.new("-2")
expect(tokenizer.tokens.map(&:class)).to match_array([
Keisan::Tokens::ArithmeticOperator,
Keisan::T... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/default_functions_spec.rb | Ruby | mit | 87 | master | 13,176 | require "spec_helper"
RSpec.describe Keisan::Functions::DefaultRegistry do
let(:registry) { described_class.registry }
it "contains correct functions" do
expect(registry["sin"].name).to eq "sin"
expect(registry["sin"].call(nil, 1).value).to eq Math.sin(1)
end
it "is unmodifiable" do
expect { regis... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/context_spec.rb | Ruby | mit | 87 | master | 13,285 | require "spec_helper"
RSpec.describe Keisan::Context do
it "contains function and variable registries" do
my_context = described_class.new
my_context.register_variable!("x", 2)
my_context.register_function!("f", Proc.new {|x| x**2})
expect(my_context.variable("x").value).to eq 2
expect(my_contex... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/turing_machine_spec.rb | Ruby | mit | 87 | master | 3,350 | require "spec_helper"
RSpec.describe "Turing machine" do
let(:calculator) { Keisan::Calculator.new }
before do
calculator.evaluate(<<-KEISAN
tape_positive = [0]
tape_negative = []
tape_index(index) = if (index >= 0, index, index.abs() ... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/functions/registry_spec.rb | Ruby | mit | 87 | master | 2,236 | require "spec_helper"
RSpec.describe Keisan::Functions::Registry do
let(:functions) { {} }
let(:parent) { nil }
let(:use_defaults) { true }
let(:registry) { described_class.new(functions: functions, parent: parent, use_defaults: use_defaults) }
context "with no parent, and using defaults" do
it "raises ... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/functions/filter_spec.rb | Ruby | mit | 87 | master | 2,883 | require "spec_helper"
RSpec.describe Keisan::Functions::Filter do
it "should be in the default context" do
c = Keisan::Context.new
expect(c.function("filter")).to be_a(described_class)
end
it "filters the list given the logical expression" do
expect{Keisan::Calculator.new.evaluate("filter(10, x, x >... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/functions/while_spec.rb | Ruby | mit | 87 | master | 3,119 | require "spec_helper"
RSpec.describe Keisan::Functions::While do
it "does loops" do
calculator = Keisan::Calculator.new
calculator.evaluate("x = 0")
calculator.evaluate("while(x < 10, x = x + 1)")
expect(calculator.evaluate("x")).to eq 10
calculator.evaluate(<<-KEISAN
in... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/functions/if_spec.rb | Ruby | mit | 87 | master | 3,240 | require "spec_helper"
RSpec.describe Keisan::Functions::If do
it "should be in the default context" do
c = Keisan::Context.new
expect(c.function("if")).to be_a(described_class)
end
it "can use logical values from variables" do
calculator = Keisan::Calculator.new
calculator.evaluate("a = true")
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/functions/proc_function_spec.rb | Ruby | mit | 87 | master | 422 | require "spec_helper"
RSpec.describe Keisan::Functions::ProcFunction do
it "can be initialized from a proc" do
function = described_class.new("test", Proc.new {|x,y| x + 3*y})
expect(function.name).to eq "test"
expect(function.call(nil,2,6).value).to eq 2 + 3*6
end
it "must be a proc" do
expect ... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/variables/registry_spec.rb | Ruby | mit | 87 | master | 1,860 | require "spec_helper"
RSpec.describe Keisan::Variables::Registry do
let(:variables) { {} }
let(:parent) { nil }
let(:use_defaults) { true }
let(:registry) { described_class.new(variables: variables, parent: parent, use_defaults: use_defaults) }
context "with no parent, and using defaults" do
it "raises ... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/ast/function_spec.rb | Ruby | mit | 87 | master | 221 | require "spec_helper"
RSpec.describe Keisan::AST::Function do
describe "is_constant?" do
it "is false" do
ast = Keisan::Calculator.new.ast("f(1)")
expect(ast.is_constant?).to eq false
end
end
end |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/ast/node_spec.rb | Ruby | mit | 87 | master | 28,239 | require "spec_helper"
RSpec.describe Keisan::AST::Node do
describe "value and evaluate" do
it "raises NotImplementedError error" do
expect{described_class.new.value}.to raise_error(Keisan::Exceptions::NotImplementedError)
expect{described_class.new.evaluate}.to raise_error(Keisan::Exceptions::NotImpl... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/ast/unary_identity_spec.rb | Ruby | mit | 87 | master | 1,158 | require "spec_helper"
RSpec.describe Keisan::AST::UnaryIdentity do
describe "simplify" do
it "eliminates UnaryIdentity" do
ast = Keisan::AST::UnaryIdentity.new([Keisan::AST::Variable.new("x")])
expect(ast.simplified).to be_a(Keisan::AST::Variable)
expect(ast.simplified.name).to eq "x"
end
... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/ast/function_assignment_spec.rb | Ruby | mit | 87 | master | 1,436 | require "spec_helper"
RSpec.describe Keisan::AST::FunctionAssignment do
describe "unbound_variables" do
it "shadows the appropriate enumerable method variables" do
ast = Keisan::AST.parse("double(a) = a.map(x, 2*x)")
expect(ast.unbound_variables).to eq Set["a"]
ast = Keisan::AST.parse("triple(... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/ast/boolean_spec.rb | Ruby | mit | 87 | master | 3,841 | require "spec_helper"
RSpec.describe Keisan::AST::Boolean do
describe "evaluate" do
it "reduces to a single number when using arithmetic operators" do
ast = Keisan::AST.parse("true && false")
expect(ast.evaluate).to be_a(described_class)
expect(ast.evaluate.value).to eq false
end
end
d... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/ast/operator_spec.rb | Ruby | mit | 87 | master | 5,291 | require "spec_helper"
RSpec.describe Keisan::AST::Operator do
shared_examples "an operator object" do |operator_class, arity, priority, associativity|
it "has the given arity" do
expect(operator_class.arity).to eq arity
end
it "has the given priority" do
expect(operator_class.priority).to eq... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/ast/builder_spec.rb | Ruby | mit | 87 | master | 5,452 | require "spec_helper"
RSpec.describe Keisan::AST::Builder do
let(:my_context) {
my_context = Keisan::Context.new
my_context.register_variable!("x", 5)
my_context
}
context "simple operations" do
operations = {
"-5" => -5,
"1 + 2" => 3,
"7.5 - 3"... |
github | project-eutopia/keisan | https://github.com/project-eutopia/keisan | spec/keisan/ast/times_spec.rb | Ruby | mit | 87 | master | 518 | require "spec_helper"
RSpec.describe Keisan::AST::Times do
it "is left associative" do
ast = Keisan::AST.parse("x * 5 * f(y) * (1*2) * zzz")
expect(ast.to_s).to eq ("(((x*5)*f(y))*(1*2))*zzz")
simple = ast.simplified
expect(simple).to be_a(Keisan::AST::Times)
expect(simple.children.map(&:class))... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.