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

Copyright © 2016 Matteo Ragni

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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, 7]
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


535
536
537
# File 'lib/functions/fnc-base.rb', line 535

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


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

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


184
185
186
# File 'lib/functions/fnc-trig.rb', line 184

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


507
508
509
# File 'lib/functions/fnc-trig.rb', line 507

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


310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/functions/fnc-box-conditions.rb', line 310

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`


131
132
133
134
135
136
137
138
# File 'lib/numbers/constants.rb', line 131

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


270
271
272
# File 'lib/functions/fnc-trig.rb', line 270

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


216
217
218
219
# File 'lib/numbers/functions.rb', line 216

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

.equal(x, y) ⇒ Object

Shortcut creates a ‘CAS::Equal` object



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

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


115
116
117
# File 'lib/functions/fnc-trsc.rb', line 115

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


148
149
150
151
152
153
154
# File 'lib/Mr.CAS/graphviz.rb', line 148

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



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

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

.greater_equal(x, y) ⇒ Object

Shortcut creates a ‘CAS::GreaterEqual` object



325
326
327
# File 'lib/functions/fnc-conditions.rb', line 325

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


450
451
452
# File 'lib/functions/fnc-base.rb', line 450

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


210
211
212
# File 'lib/functions/fnc-trsc.rb', line 210

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


197
198
199
# File 'lib/functions/fnc-piecewise.rb', line 197

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


206
207
208
# File 'lib/functions/fnc-piecewise.rb', line 206

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


193
194
195
# File 'lib/functions/fnc-base.rb', line 193

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


107
108
109
# File 'lib/functions/fnc-trig.rb', line 107

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

.smaller(x, y) ⇒ Object

Shortcut creates ‘CAS::Smaller` object



330
331
332
# File 'lib/functions/fnc-conditions.rb', line 330

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

.smaller_equal(x, y) ⇒ Object

Shortcut creates a ‘CAs::SmallerEqual` object



335
336
337
# File 'lib/functions/fnc-conditions.rb', line 335

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


365
366
367
# File 'lib/functions/fnc-base.rb', line 365

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


428
429
430
# File 'lib/functions/fnc-trig.rb', line 428

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`


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/Mr.CAS/graphviz.rb', line 107

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`


217
218
219
220
221
222
223
224
# File 'lib/numbers/variables.rb', line 217

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