Module: CAS

Defined in:
lib/Mr.CAS.rb,
lib/Mr.CAS.rb,
lib/version.rb,
lib/Mr.CAS/c.rb,
lib/Mr.CAS/c-opt.rb,
lib/Mr.CAS/latex.rb,
lib/operators/op.rb,
lib/Mr.CAS/graphviz.rb,
lib/Mr.CAS/auto-diff.rb,
lib/functions/fnc-sum.rb,
lib/numbers/constants.rb,
lib/numbers/functions.rb,
lib/numbers/variables.rb,
lib/operators/bary-op.rb,
lib/operators/nary-op.rb,
lib/functions/fnc-base.rb,
lib/functions/fnc-prod.rb,
lib/functions/fnc-trig.rb,
lib/functions/fnc-trsc.rb,
lib/functions/fnc-piecewise.rb,
lib/functions/fnc-conditions.rb,
lib/functions/fnc-box-conditions.rb

Overview

_ _ _ _ _ _

/ __|_ _ __ _ _ __| |___ _(_)___ | _ \ |_  _ __ _(_)_ _

| (_ | ‘_/ _` | ’_ \ ‘ \ V / |_ / | _/ | || / _` | | ’ \

\___|_| \__,_| .__/_||_\_/|_/__| |_| |_|\_,_\__, |_|_||_|
             |_|                            |___/

Defined Under Namespace

Modules: AutoDiff, C_PLUGIN, Help Classes: Abs, Acos, Asin, Atan, BinaryOp, BoxCondition, BoxConditionClosed, BoxConditionLowerClosed, BoxConditionOpen, BoxConditionUpperClosed, CASError, CLib, Condition, Constant, Cos, Diff, Div, E_CONSTANT, Equal, Exp, Function, Greater, GreaterEqual, INFINITY_CONSTANT, Invert, Ln, MINUS_ONE_CONSTANT, Max, Min, MinMax, NEG_INFINITY_CONSTANT, NaryOp, ONE_CONSTANT, Op, PI_CONSTANT, Piecewise, Pow, Prod, Sin, Smaller, SmallerEqual, Sqrt, Sum, TWO_CONSTANT, Tan, Variable, ZERO_CONSTANT

Constant Summary collapse

VERSION =

Version of the library Array of three ‘Fixnum` values:

* Major version
* Minor version
* Patchlevel
[0, 2, 6]
Zero =

Zero (0) constant representation

CAS::ZERO_CONSTANT.new
One =

One (1) constant representation

CAS::ONE_CONSTANT.new
Two =

Two (2) constant representation

CAS::TWO_CONSTANT.new
Pi =

Pi (3.14…) constant representation

CAS::PI_CONSTANT.new
E =

E (2.57…) constant representation

CAS::E_CONSTANT.new
Infinity =

Infinity constant representation

CAS::INFINITY_CONSTANT.new
NegInfinity =

Negative Infinity constant representation

CAS::NEG_INFINITY_CONSTANT.new
MinusOne =

Minus One (-1) constant representation

CAS::MINUS_ONE_CONSTANT.new
NumericToConst =

Series of useful numeric constant, Based upon ‘Numeric` keys, with `CAs::Constant` value

{
  0 => CAS::Zero,
  0.0 => CAS::Zero,
  1 => CAS::One,
  1.0 => CAS::One,
  2 => CAS::Two,
  2.0 => CAS::Two,
  Math::PI => CAS::Pi,
  Math::E => CAS::E,
  (1.0/0.0) => CAS::Infinity,
}

Class Method Summary collapse

Class Method Details

.abs(x) ⇒ Object

Shortcut for ‘CAs::Abs` initializer

* **argument**: `CAs::Op` argument of absolute value
* **returns**: `CAS::Abs` new instance


512
513
514
# File 'lib/functions/fnc-base.rb', line 512

def self.abs(x)
  CAS::Abs.new x
end

.acos(x) ⇒ Object Also known as: arccos

Shortcut for ‘CAS::Acos#new`

* **argument**: `CAS::Op` argument of the function
* **returns**: `CAS::Acos` operation


319
320
321
# File 'lib/functions/fnc-trig.rb', line 319

def acos(x)
  CAS::Acos.new x
end

.asin(x) ⇒ Object Also known as: arcsin

Shortcuts for ‘CAS::Asin#new`

* **argument**: `CAS::Op` argument of the function
* **returns**: `CAS::Asin` operation


161
162
163
# File 'lib/functions/fnc-trig.rb', line 161

def asin(x)
  CAS::Asin.new x
end

.atan(x) ⇒ Object Also known as: arctan

Shortcut for ‘CAS::Atan#new`

* **argument**: `CAS::Op` argument of the function
* **returns**: `CAS::Atan` operation


484
485
486
# File 'lib/functions/fnc-trig.rb', line 484

def atan(x)
  CAS::Atan.new x
end

.box(x, a, b, type = :closed) ⇒ Object Also known as: in

Shortcut for creating a new box condition. It requires four arguments:

* **argument**: `CAS::Op` function for condition
* **argument**: `CAS::Constant` lower limit
* **argument**: `CAs::Constant` upper limit
* **argument**: `Symbol` of condition type it can be:
   - `:closed` for `CAs::BoxConditionClosed`
   - `:open` for `CAs::BoxConditionOpen`
   - `:upper_closed` for `CAs::BoxConditionUpperClosed`
   - `:lower_closed` for `CAs::BoxConditionLowerClosed`
* **returns**: `CAS::BoxCondition` new instance


287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/functions/fnc-box-conditions.rb', line 287

def box(x, a, b, type=:closed)
  case type
  when :closed
    return CAS::BoxConditionClosed.new(x, a, b)
  when :open
    return CAS::BoxConditionOpen.new(x, a, b)
  when :upper_closed
    return CAS::BoxConditionUpperClosed.new(x, a, b)
  when :lower_closed
    return CAS::BoxConditionLowerClosed.new(x, a, b)
  else
    raise CAS::CASError, "Unknown box condition type"
  end
end

.const(*val) ⇒ Object

Allows to define a series of new constants.

“‘ ruby a, b = CAS::const 1.0, 100 “`

* **argument**: `Array` of Numeric
* **returns**: `Array` of `CAS::Contant`


108
109
110
111
112
113
114
115
# File 'lib/numbers/constants.rb', line 108

def self.const(*val)
  #(val = [val]) if val.size == 1
  ret = []
  val.each do |n|
    ret << (NumericToConst[n] ? NumericToConst[n] : CAS::Constant.new(n))
  end
  return (ret.size == 1 ? ret[0] : ret)
end

.cos(x) ⇒ Object

Shortcut for ‘CAS::Cos#new`

* **argument**: `CAS::Op` argument of the function
* **returns**: `CAS::Cos` operation


247
248
249
# File 'lib/functions/fnc-trig.rb', line 247

def self.cos(x)
  CAS::Cos.new x
end

.declare(name, *xs) ⇒ Object

This shortcut allows to declare a new function

* **requires**: `String` or `Symbol` that is the name of the function
* **requires**: `Array` of `CAS::Variable`
* **returns**: a new `CAS::Function` or the old one


189
190
191
192
# File 'lib/numbers/functions.rb', line 189

def declare(name, *xs)
  xs.flatten!
  CAS::Function.new(name, xs)
end

.equal(x, y) ⇒ Object

Shortcut creates a ‘CAS::Equal` object



300
301
302
# File 'lib/functions/fnc-conditions.rb', line 300

def self.equal(x, y)
  CAS::Equal.new(x, y)
end

.exp(x) ⇒ Object

Shortcut for ‘CAS::Exp#new`

* **argument**: `CAS::Op` argument of the function
* **returns**: `CAS::Exp` operation


92
93
94
# File 'lib/functions/fnc-trsc.rb', line 92

def self.exp(x)
  CAS::Exp.new x
end

.export_dot(fl, op) ⇒ Object

Export the input ‘CAS::Op` graphviz representation to a file.

* **argument**: `String` with filename
* **argument**: `CAS::Op` with the tree
* **returns**: `CAS::Op` in input


125
126
127
128
129
130
131
# File 'lib/Mr.CAS/graphviz.rb', line 125

def self.export_dot(fl, op)
  CAS::Help.assert(fl, String)
  CAS::Help.assert(op, CAS::Op)

  File.open(fl, "w") do |f| f.puts CAS.to_dot(op) end
  return op
end

.greater(x, y) ⇒ Object

Shortcut creates a ‘CAS::Greater` object



305
306
307
# File 'lib/functions/fnc-conditions.rb', line 305

def self.greater(x, y)
  CAS::Greater.new(x, y)
end

.greater_equal(x, y) ⇒ Object

Shortcut creates a ‘CAS::GreaterEqual` object



310
311
312
# File 'lib/functions/fnc-conditions.rb', line 310

def self.greater_equal(x, y)
  CAS::GreaterEqual.new(x, y)
end

.invert(x) ⇒ Object

Shortcut for ‘CAs::Invert` initializer

* **argument**: `CAs::Op` argument of the inversion
* **returns**: `CAS::Invert` new instance


427
428
429
# File 'lib/functions/fnc-base.rb', line 427

def self.invert(x)
  CAS::Invert.new x
end

.ln(x) ⇒ Object Also known as: log

Shortcut for ‘CAS::Ln#new`

* **argument**: `CAS::Op` argument of the function
* **returns**: `CAS::Ln` operation


187
188
189
# File 'lib/functions/fnc-trsc.rb', line 187

def ln(x)
  CAS::Ln.new x
end

.max(x, y) ⇒ Object

Shortcut for ‘CAS::Max` initializer

* **argument**: `CAS::Op` left function
* **argument**: `CAS::Op` right function
* **returns**: `CAS::Max` new instance


174
175
176
# File 'lib/functions/fnc-piecewise.rb', line 174

def self.max(x, y)
  CAS::Max.new(x, y)
end

.min(x, y) ⇒ Object

Shortcut for ‘CAS::Min` initializer

* **argument**: `CAS::Op` left function
* **argument**: `CAS::Op` right function
* **returns**: `CAS::Min` new instance


183
184
185
# File 'lib/functions/fnc-piecewise.rb', line 183

def self.min(x, y)
  CAS::Min.new(x, y, CAS::smaller_equal(x, y))
end

.pow(x, y) ⇒ Object

Shortcut for ‘CAS::Pow` initializer

* **argument**: `CAS::Op` base
* **argument**: `CAS::Op` exponent
* **returns**: `CAS::Pow` new instance


170
171
172
# File 'lib/functions/fnc-base.rb', line 170

def self.pow(x, y)
  CAS::Pow.new x, y
end

.sin(x) ⇒ Object

Shortcut for ‘CAS::Sin#new`

* **argument**: `CAS::Op` argument of the function
* **returns**: `CAS::Sin` operation


84
85
86
# File 'lib/functions/fnc-trig.rb', line 84

def self.sin(x)
  CAS::Sin.new x
end

.smaller(x, y) ⇒ Object

Shortcut creates ‘CAS::Smaller` object



315
316
317
# File 'lib/functions/fnc-conditions.rb', line 315

def self.smaller(x, y)
  CAS::Smaller.new(x, y)
end

.smaller_equal(x, y) ⇒ Object

Shortcut creates a ‘CAs::SmallerEqual` object



320
321
322
# File 'lib/functions/fnc-conditions.rb', line 320

def self.smaller_equal(x, y)
  CAS::SmallerEqual.new(x, y)
end

.sqrt(x) ⇒ Object

Shortcut for ‘CAS::Sqrt` initializer

* **argument**: `CAS::Op` argument of square root
* **returns**: `CAS::Sqrt` new instance


342
343
344
# File 'lib/functions/fnc-base.rb', line 342

def self.sqrt(x)
  CAS::Sqrt.new x
end

.tan(x) ⇒ Object

Shortcut for ‘CAS::Tan#new`

* **argument**: `CAS::Op` argument of the function
* **returns**: `CAS::Tan` operation


405
406
407
# File 'lib/functions/fnc-trig.rb', line 405

def self.tan(x)
  CAS::Tan.new x
end

.to_dot(op) ⇒ Object

Return a string representation of the graph that is a Graphviz tree. Requires a ‘CAS::Op` as argument. In the next releases probably it will be moved inside `CAS::Op`.

* **argument**: `CAS::Op` instance
* **returns**: `String`


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/Mr.CAS/graphviz.rb', line 84

def self.to_dot(op)
  CAS::Help.assert(op, CAS::Op)
  string = op.dot_graph
  labels = ""

  dot_subs_hash = {
    "Sum"                => "+",
    "Diff"               => "-",
    "Prod"               => "×",
    "Div"                => "÷",
    "Sqrt"               => "√(∙)",
    "Abs"                => "|∙|",
    "Invert"             => "-(∙)",
    "Exp"                => "exp(∙)",
    "Log"                => "log(∙)",
    "Pow"                => "(∙)^(∙)",
    "ZERO_CONSTANT"      => "0",
    "ONE_CONSTANT"       => "1",
    "TWO_CONSTANT"       => "2",
    "PI_CONSTANT"        => "π",
    "INFINITY_CONSTANT"  => "",
    "E_CONSTANT"         => "e",
    "MINUS_ONE_CONSTANT" => "-1"
  }

  lab = {}
  string.scan(/\w+\_\d+/) do |m|
    if m =~ /(\w+)\_\d+/
      lab[m] = dot_subs_hash[$1] || $1
    end
  end
  lab.each { |k, v| labels += "  #{k} [label=\"#{v}\"]\n" }

  return "digraph Op {\n  #{string}#{labels}}"
end

.vars(*name) ⇒ Object

Allows to define a series of new variables.

“‘ ruby x, y = CAS::vars :x, :y “`

* **argument**: `Array` of Numeric
* **returns**: `Array` of `CAS::Variable`


194
195
196
197
198
199
200
201
# File 'lib/numbers/variables.rb', line 194

def self.vars(*name)
  (return CAS::Variable.new(name[0])) if name.size == 1
  ret = []
  name.each do |n|
    ret << CAS::Variable.new(n)
  end
  return ret
end