epimath100 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dfc33b95a013088913670b6a6dad2b4bdec2d199
4
+ data.tar.gz: edb093c443f19cb614ae2c754cffa59d047af727
5
+ SHA512:
6
+ metadata.gz: 5613ff9bee2e2234790239a4b4db1c017124ac54933c6601c67720056e8b563d6e7caf60fbda5dfb3df0351988c4993186d643538a98f29ceb9e663e8e9a62d7
7
+ data.tar.gz: f997b826a3534bdfe8d303c0cc9fd281168e4ac0431b06d9ffbf4f2321de265b46f63192ed6acf886c9e6bf23ae45ecba77e1010029a611a56d4dc599e67e94d
@@ -0,0 +1,100 @@
1
+ #encoding: utf-8
2
+
3
+ =begin
4
+ Error is a class to display and manage your simple errors
5
+ =end
6
+
7
+
8
+ class Error
9
+ ERR_HIGH = "Fatal Error"
10
+ ERR_MEDIUM = "Error"
11
+ ERR_LOW = "Warning"
12
+ ERR_COLOR_RED = "0;31"
13
+ ERR_COLOR_GREEN = "0;32"
14
+ ERR_COLOR_YELLOW = "1;33"
15
+ ERR_COLOR_BLUE = "0;34"
16
+ ERR_COLOR_ORANGE = "0;33"
17
+ @@errors = 0
18
+
19
+ # The function will check if the specified value can be converted to a Numerical value.
20
+ # == Parameters:
21
+ # type::
22
+ # ...
23
+ # == Returns:
24
+ # True/False
25
+ def self.isnum? string
26
+ if string.is_a?String and string != nil
27
+ if string.to_i.to_s == string or string.to_f.to_s == string
28
+ return true
29
+ elsif string.to_i.to_s == string[1..-1] or string.to_f.to_s == string[1..-1]
30
+ true
31
+ else
32
+ return false
33
+ end
34
+ elsif string.is_a?Numeric
35
+ true
36
+ else
37
+ Error.call "'#{string}' is not a String"
38
+ return false
39
+ end
40
+ end
41
+
42
+ # "call" is a function you can acces with:
43
+ # Error.call "message", ERR_LEVEL
44
+ # == The error's levels are :
45
+ # * ERR_HIGH
46
+ # * ERR_MEDIUM
47
+ # * ERR_LOW
48
+ # The error's level influence the color (visibility) and defined if the programm must exit.
49
+ # An ERR_HIGH is only to call exit and stop the programm. So be carrefull.
50
+ # ERR_MEDIUM and ERR_LOW will just display the message and no more.
51
+ # ERR_HIGH is the default value, you can change it if yo want
52
+ # def self.call m, level=ERR_MEDIUM
53
+ #
54
+ # == Parameters:
55
+ # m::
56
+ # A String that will be display. You don't need to specify the prefix ("Error :") or the final "."
57
+ # level::
58
+ # A optional parameter, wich influence the degree of the error. Default is ERR_HIGH, will kill your programm
59
+ # So, be carrefull. You can change this value. It may be ERR_HIGH (Default), ERR_MEDIUM, ERR_LOW
60
+ #
61
+ # == Returns:
62
+ # nil
63
+ def self.call m, level=Error::ERR_HIGH
64
+
65
+ if level != Error::ERR_HIGH and level != Error::ERR_MEDIUM and level != Error::ERR_LOW
66
+ self.call "Error::call : error level invalid", Error::ERR_MEDIUM
67
+ end
68
+
69
+ #message color
70
+ if level == Error::ERR_HIGH
71
+ color = Error::ERR_COLOR_RED
72
+ elsif level == Error::ERR_MEDIUM
73
+ color = Error::ERR_COLOR_ORANGE
74
+ elsif level == Error::ERR_LOW
75
+ color = Error::ERR_COLOR_YELLOW
76
+ end
77
+
78
+ #message display
79
+ if level == Error::ERR_HIGH
80
+ puts "\033[#{color}m#{level} : #{m}.\033[00m"
81
+ exit
82
+ elsif level == Error::ERR_MEDIUM
83
+ puts "\033[#{color}m#{level} : #{m}.\033[00m"
84
+ elsif level == Error::ERR_LOW
85
+ puts "\033[#{color}m#{level} : #{m}.\033[00m"
86
+ end
87
+
88
+ @@errors += 1
89
+ return self
90
+ end
91
+
92
+ # == Parameters:
93
+ # nothing::
94
+ #
95
+ # == Returns:
96
+ # Integer which contains the numbr of errors called
97
+ def self.errors
98
+ @@errors
99
+ end
100
+ end
@@ -0,0 +1,105 @@
1
+ #encoding: utf-8
2
+
3
+ require_relative 'error.class.rb'
4
+ require_relative 'point.class.rb'
5
+ require_relative 'vector.class.rb'
6
+
7
+ class Line
8
+ attr_reader :point, :v_dir, :equ_para
9
+
10
+ # == Parameters:
11
+ # point::
12
+ # Any point on the line. It must be a Point (../point/point.class.rb)
13
+ # vector::
14
+ # Any vector director of the line. It must be a Vector (../vector/vector.class.rb)
15
+ def initialize point, vector
16
+ Error.call "Line::new : '#{point}' is not a Point" if !point.is_a?Point
17
+ Error.call "Line::new : '#{vector}' is not a Vector" if !vector.is_a?Vector
18
+ Error.call "Line::new : '#{vector}' can't be Null" if vector.nil?
19
+
20
+ @point = point
21
+ @v_dir = vector
22
+ @equ_para = Line::parametric @point, @v_dir
23
+ end
24
+
25
+ # TODO : fix if if if if if
26
+ # Check if the point specified is ON the line
27
+ # == Parameter:
28
+ # p::
29
+ # p is a Point
30
+ # == Returns:
31
+ # true/false
32
+ def point_owned? p
33
+ Error.call "Line::point_owned? : #{p} is not a valid Point" if !p.is_a?Point
34
+
35
+ l = parametric()
36
+
37
+ ux = (p.x - l[:x][:p]) / l[:x][:v]
38
+ uy = (p.y - l[:y][:p]) / l[:y][:v]
39
+ uz = (p.z - l[:z][:p]) / l[:z][:v]
40
+
41
+ if l[:x][:v] == 0 and p.x == l[:x][:p]
42
+ ux = uy
43
+ end
44
+
45
+ if l[:y][:v] == 0 and p.y == l[:y][:p]
46
+ uy = uz
47
+ end
48
+
49
+ if l[:z][:v] == 0 and p.z == l[:z][:p]
50
+ uz = ux
51
+ end
52
+
53
+ if ux == uy and ux == uz
54
+ true
55
+ else
56
+ false
57
+ end
58
+ end
59
+
60
+ # == Returns::
61
+ # same than self.parametric but with the current object
62
+ def parametric
63
+ return Line::parametric @point, @v_dir
64
+ end
65
+
66
+ # == Parameters:
67
+ # point::
68
+ # point is a Point on the line
69
+ # v_dir::
70
+ # a vector director of the line
71
+ # == Returns:
72
+ # Hash
73
+ def self.parametric point, v_dir
74
+ Error.call "Line::parametric : Invalid vector" if !v_dir.is_a?Vector
75
+ Error.call "Line::parametric : Invalid point" if !point.is_a?Point
76
+
77
+ þ = 1
78
+ { :x => { :p => point.x, :v => þ * v_dir.x},
79
+ :y => { :p => point.y, :v => þ * v_dir.y},
80
+ :z => { :p => point.z, :v => þ * v_dir.z}}
81
+ end
82
+
83
+ def to_s(options={})
84
+ if options == {}
85
+ "droite passant par le point (#{@point.x};#{@point.y};#{@point.z}), de vecteur directeur #{@v_dir.to_s}"
86
+ else
87
+ Error.call "Line::to_s : options is not valid"
88
+ end
89
+ end
90
+
91
+ # This function returns the point on the line where x = v * vx + px, ...
92
+ # == Parameters:
93
+ # v::
94
+ # The value defined the point wich will be return.
95
+ # == Returns:
96
+ # The choosed Point one the line from v (lambda)
97
+ def function v
98
+ Error.call "Line::function : unable to execute function : the vector v_dir is null" if self.nil?
99
+ Error.call "Line::functionx : invalid lambda ( #{v} ) value" if !v.is_a?Numeric
100
+
101
+ lp = parametric()
102
+ Point.new(lp[:x][:p] + v * lp[:x][:v], lp[:y][:p] + v * lp[:y][:v], lp[:z][:p] + v * lp[:z][:v])
103
+ end
104
+
105
+ end
@@ -0,0 +1,307 @@
1
+ #encoding: utf-8
2
+
3
+ require_relative 'error.class'
4
+ require_relative 'vector.class'
5
+
6
+ # TODO : improve the documentation
7
+ class Matrix
8
+ attr_reader :columns, :lines, :v
9
+
10
+ # Convert each element of an Array in an Integer => must be implented in Array Class
11
+ def self.to_inttab arg, num=Float
12
+ Error.call "Matrix::to_inttab : '#{arg}' is not a valid Array", Error::ERR_HIGH if !arg.is_a?Array
13
+
14
+ arg.size.times do |i|
15
+ # if it's an Array in an Array, the
16
+ if arg[i].is_a?Array
17
+ arg[i] = self.to_inttab argv[i]
18
+
19
+ else
20
+ if num == Integer
21
+ arg[i] = arg[i].to_i
22
+ else
23
+ arg[i] = arg[i].to_f
24
+ end
25
+ end
26
+
27
+ end
28
+ end
29
+
30
+ # == Parameters:
31
+ # tab::
32
+ # tab is a double Array like [ [1,2], [3,4], [1,4] ]. This array should be a valid Matrix tab or an other Matrix
33
+ #
34
+ # == Returns:
35
+ # nothing
36
+ def initialize tab=[[]]
37
+ @v = tab #Must call to_inttab
38
+ @columns = 0
39
+ @lines = 0
40
+
41
+ if tab.is_a?Array and tab.size > 0 and tab[0].is_a?Array
42
+ @columns = tab[0].size
43
+
44
+ # check if the line have the good size
45
+ tab.size.times do |i|
46
+ @lines += 1
47
+ if (tab[i].size != @columns)
48
+ Error.call "Matrix::new : '#{tab[i]}' is not a valid line"
49
+ end
50
+
51
+ end
52
+ elsif tab.is_a?Matrix
53
+ @v = tab.v
54
+ @columns = tab.columns
55
+ @lines = tab.lines
56
+ else
57
+ Error.call "Matrix::new : '#{tab}' is not a valid matrix"
58
+ end
59
+ return
60
+ end
61
+
62
+ def to_s
63
+ out = ""
64
+
65
+ @v.each do |line|
66
+ out << "["
67
+
68
+ # display all elements of this line
69
+ line.each do |element|
70
+ out << element.to_f.round(3).to_s << " "
71
+ end
72
+
73
+ out << "\b]\n" # TODO : FIX THAT broggi_t
74
+ end
75
+ out
76
+ end
77
+
78
+ def to_ary
79
+ @v
80
+ end
81
+
82
+ def to_vector
83
+ return Vector.new self.get_val(0, 0), self.get_val(1, 0)
84
+ end
85
+
86
+ def new_line tab=[]
87
+ Error.call "Matrix::new_line : Size of the new line (#{tab} => #{tab.size}) is not valid" if !tab.is_a?Array or tab.size != @column
88
+
89
+ @lines += 1
90
+ @v << tab
91
+ end
92
+
93
+ def new_column tab=[]
94
+ Error.call "Matrix::new_column : Size of the new column (#{tab} => #{tab.size}) is not valid" if !tab.is_a?Array or tab.size != @lines
95
+
96
+ @columns += 1
97
+ if tab.is_a? Array and tab.size == @lines
98
+ @lines.times do |i|
99
+ @v[i] << tab[i]
100
+ end
101
+ else
102
+ @lines.times do |i|
103
+ @v[i] << 0
104
+ end
105
+ end
106
+ end
107
+
108
+ def del_line x=-1
109
+ if x.is_a?Integer and @v[x] != nil
110
+ @lines -= 1
111
+ @v.delete_at x
112
+ else
113
+ Error.call "Matrix::del_line : Line '#{x}' doesn't exist"
114
+ end
115
+ end
116
+
117
+ # == Parameters:
118
+ # x,y::
119
+ # Integers. They are the coordonates of the value which will extract from the matrix
120
+ # == Returns:
121
+ # a value fo the matrix
122
+ def get_val x, y
123
+ if !x.is_a?Integer
124
+ Error.call "Matrix::get_val : '#{x}' is not a correct line"
125
+ return nil
126
+ elsif !y.is_a?Integer
127
+ Error.call "Matrix::get_val : '#{y}' is not a correct column"
128
+ return nil
129
+ elsif x < 0 or y < 0 or x >= @lines or y >= @columns
130
+ Error.call "Matrix::get_val : The specified positions are invalids (#{x},#{y})"
131
+ return nil
132
+ else
133
+ return @v[x][y]
134
+ end
135
+ end
136
+
137
+ # == Parameters:
138
+ # x,y::
139
+ # Integers. They are the coordonates of the value which will write in the matrix
140
+ # == Returns:
141
+ # a value fo the matrix
142
+ def set_val val, x, y
143
+ if !x.is_a?Integer
144
+ Error.call "Matrix::set_val : '#{x}' is not a correct line"
145
+ return nil
146
+ elsif !y.is_a?Integer
147
+ Error.call "Matrix::set_val : '#{y}' is not a correct column"
148
+ return nil
149
+ elsif !val.is_a?Numeric
150
+ Error.call "Matrix::set_val : '#{val}' is not a correct value"
151
+ return nil
152
+ elsif x < 0 or y < 0 or x >= @lines or y >= @columns
153
+ Error.call "Matrix::set_val : The specified positions are invalids (#{x} >= #{@lines},#{y} >= #{@columns})\n#{self.to_s}"
154
+ return nil
155
+ else
156
+ @v[x][y] = val
157
+ return @v[x][y]
158
+ end
159
+ end
160
+
161
+ # == Parameters:
162
+ # y::
163
+ # Integer. It's the n° line which is extracted
164
+ # == Returns:
165
+ # Array
166
+ def get_line x
167
+ Error.call "Matrix::get_line : Line #{x} doesn't exist" if !x.is_a?Integer or x < 0 or x >= @lines
168
+
169
+ return @v[x]
170
+ end
171
+
172
+ # == Parameters:
173
+ # y::
174
+ # Integer. It's the n° column which is extracted
175
+ # == Returns:
176
+ # Array
177
+ def get_column y
178
+ Error.call "Matrix::get_column : Column #{y} doesn't exist" if !y.is_a?Integer or y < 0 or y >= @columns
179
+
180
+ result = []
181
+ @lines.times do |i|
182
+ result << @v[i][y]
183
+ end
184
+ return result
185
+ end
186
+
187
+ # == Params:
188
+ # matrix::
189
+ # matrix is a Matrix to compare.
190
+ # == Returns:
191
+ # True or False.
192
+ # == Usage::
193
+ # The function check if the current matrix and matrix:: have the same dimensions (linse and columns)
194
+ def have_the_same_dimensions matrix
195
+ if (matrix.is_a?Matrix and matrix.columns == @columns and matrix.lines == @lines)
196
+ true
197
+ else
198
+ false
199
+ end
200
+ end
201
+
202
+ # == Parameters:
203
+ # t1,t2::
204
+ # Multiply each elements of t1 and t2 2b2 and sum all
205
+ # == Returns:
206
+ # Float
207
+ def self.mult_array(t1, t2)
208
+ Error.call "Can't multiply this. One of the arguments is not an array.", Error::ERR_HIGH if (!t1.is_a?Array or !t2.is_a?Array)
209
+ Error.call "Can't multiply this. Arrays do not have the same size.", Error::ERR_HIGH if t1.size != t2.size
210
+
211
+ result = 0.0
212
+ t1.size.times do |i|
213
+ result = (result + t1[i].to_f * t2[i].to_f).to_f
214
+ end
215
+ return result
216
+ end
217
+
218
+ # == Parameters:
219
+ # matrix::
220
+ # This argument is a Matrix or an Integer.
221
+ # If it's a Matrix, it will do matrix product.
222
+ # Else, if it's a integer, it will multiply each coeficient of the current Matrix.
223
+ #
224
+ # == Returns:
225
+ # Matrix
226
+ #
227
+ # == Matrix_Product:
228
+ # little explanation::
229
+ # If matrix is a Matrix, we will multiply 2by2 each coeficient of the column X of the current Matrix and the line X of matrix.
230
+ # Then, we do the sum of them and we put it in a new Matrix at the position X. The is just a sum up, view the details on wiki bitch.
231
+ def *(matrix)
232
+ #produit matriciel
233
+ #convert vector -> matrix
234
+ if matrix.is_a?Vector
235
+ Error.call "Matrix::* : Transformation implicite de Vector en Matrix", Error::ERR_LOW
236
+ matrix = matrix.to_matrix
237
+ end
238
+
239
+ if matrix.is_a?Matrix
240
+ Error.call "Matrix::* : Invalid multiplication at line #{matrix.lines} and column #{@columns}", Error::ERR_HIGH if @columns != matrix.lines
241
+
242
+ result = []
243
+ @lines.times do |i|
244
+ result << []
245
+ end
246
+ #colonne de resultat = colonne de matrix X
247
+ #ligne de resutlat = ligne de self Y
248
+ @lines.times do |y|
249
+ matrix.columns.times do |x|
250
+ result[y][x] = Matrix.mult_array(get_line(y), matrix.get_column(x))
251
+ end
252
+ end
253
+
254
+ return Matrix.new result
255
+ #produit d'un entier et d'une matrix
256
+ elsif matrix.is_a?Numeric
257
+ result = @v
258
+ @lines.times do |x|
259
+ @columns.times do |y|
260
+ result[x][y] = result[x][y].to_f * matrix
261
+ end
262
+ end
263
+ return Matrix.new result
264
+ #message d'erreur
265
+ else
266
+ Error.call "Matrix::* : Impossible de calculer cela (#{matrix} n'est pas une matrix)", Error::ERR_HIGH
267
+ end
268
+ end
269
+
270
+ # == Parameters:
271
+ # matrix::
272
+ # This argument is a Matrix or an Integer. If it's a Matrix, it must have the same dimensions than the current Matrix.
273
+ # Else, if it's a integer, it will be added to each coeficients of the current Matrix.
274
+ #
275
+ # == Returns:
276
+ # Matrix
277
+ def +(matrix)
278
+ result = @v
279
+ if have_the_same_dimensions matrix
280
+ @lines.times do |x|
281
+ @columns.times do |y|
282
+ result[x][y] += matrix.v[x][y]
283
+ end
284
+ end
285
+ elsif matrix.is_a?Numeric
286
+ @lines.times do |x|
287
+ @columns.times do |y|
288
+ result[x][y] += matrix
289
+ end
290
+ end
291
+ else
292
+ Error.call "Matrix::+ : Impossible de calculer cela", Error::ERR_HIGH
293
+ end
294
+ Matrix.new result
295
+ end
296
+
297
+ # == Returns::
298
+ # Numerical value which is the determinant of the matrix. It only work on 2x2
299
+ def get_deter
300
+ Error.call "Matrix::get_deter : This error comes from get_deter which works only with 2x2 matrix" if @columns != 2 or @lines != 2
301
+
302
+ det = get_val(0, 0).to_i * get_val(1, 1).to_i
303
+ det -= get_val(0, 1).to_i * get_val(1, 0).to_i
304
+ return det
305
+ end
306
+
307
+ end
@@ -0,0 +1,50 @@
1
+ #encoding: utf-8
2
+
3
+ require_relative 'error.class.rb'
4
+
5
+ class Point
6
+ def initialize x, y, z
7
+ if !x.is_a?Numeric or !y.is_a?Numeric or !z.is_a?Numeric
8
+ Error.call "Point::new : a passed argument is not a valid number"
9
+ end
10
+ @coord = {:x => x.to_f, :y => y.to_f, :z => z.to_f}
11
+ end
12
+
13
+ def +(p)
14
+ if p.is_a?Point
15
+ @coord.x += p.x
16
+ @coord.y += p.y
17
+ @coord.z += p.z
18
+ elsif p.is_a?Numeric
19
+ @coord.x += p
20
+ @coord.y += p
21
+ @coord.z += p
22
+ else
23
+ Error.call "Point::+ : passed argument is invalid"
24
+ end
25
+ end
26
+
27
+ def *(p)
28
+ Error.call "Point::+ : passed argument is invalid" if !p.is_a?Numeric
29
+
30
+ @coord.x *= p
31
+ @coord.y *= p
32
+ @coord.z *= p
33
+ end
34
+
35
+ def to_s
36
+ "(#{self.x}; #{self.y}; #{self.z})"
37
+ end
38
+
39
+ def x
40
+ @coord[:x]
41
+ end
42
+
43
+ def y
44
+ @coord[:y]
45
+ end
46
+
47
+ def z
48
+ @coord[:z]
49
+ end
50
+ end
@@ -0,0 +1,88 @@
1
+ #encoding: utf-8
2
+
3
+ require_relative 'error.class'
4
+ require_relative 'vector.class'
5
+
6
+ class Polynomial
7
+ EXPOSANT = {"0" => "⁰",
8
+ "1" => "¹",
9
+ "2" => "²",
10
+ "3" => "³",
11
+ "4" => "⁴",
12
+ "5" => "⁵",
13
+ "6" => "⁶",
14
+ "7" => "⁷",
15
+ "8" => "⁸",
16
+ "9" => "⁹"}
17
+
18
+ attr_accessor :coef
19
+
20
+ # Initialize the polynominal function
21
+ # Its coeficients are 1, 2, 3, 4 ... with '1'x⁰ + '2'x¹ + '3'x² ... = y
22
+ # Each coeficients has an associated value (exemple : 2 => 1 correspond to 1x²)
23
+ #
24
+ # == Notes:
25
+ # The function is compatible with the first version, where coeficients keys are :a, :b, ...
26
+ # == Parameters:
27
+ # hash::
28
+ # hash is a hash which have several keys 1, 2,... which correspond to the coeficients
29
+ # verb:: default is false
30
+ # verb is true or false, or a integer. It will display more information if turned on when to_s.
31
+ # if it's a integer, it must be in the list :
32
+ # - 0 : returns ""
33
+ # - 1 : "y = equation"
34
+ # - 2 : "f(x) = equation" (like true)
35
+ def initialize hash={}, verb=false
36
+ Error.call "Polynomial::new : You hash is invalid" if !hash.is_a?Hash
37
+
38
+ hash.select{|k,v| k.to_s.match(/[a-z]/)}.each do |k,v|
39
+ key = (k.to_s.ord - "a".ord)
40
+ hash[key] = v if hash[key] == nil
41
+ end
42
+
43
+ @coef = hash.select{|coef,value| coef.is_a?Numeric and coef >= 0 and value.is_a?Numeric}
44
+ @verbose = verb
45
+ end
46
+
47
+ #calculate the derivated function of the current polynomial
48
+ # == Returns:
49
+ # Polynomial (the derivated function)
50
+ def derive
51
+ dérivé = Polynomial.new
52
+
53
+ @coef.select{|coef,value| coef != 0}.each do |coef,value|
54
+ dérivé.coef[coef - 1] = value * coef
55
+ end
56
+
57
+ return dérivé
58
+ end
59
+
60
+ def to_s
61
+ return "" if @verbose == 0
62
+
63
+ str = ""
64
+ str = "#{@coef[0].to_i}" + str #if @coef[:a]
65
+
66
+ @coef.select{|coef,value| coef != 0}.each do |coef,value|
67
+ #sign = "+"
68
+ #sign = "-" if value < 0
69
+ str = "#{value}x^#{coef} + " + str if value != 0
70
+ end
71
+ str = "f(x) = " + str if @verbose == true or @verbose == 2
72
+ str = "y = " + str if @verbose == 1
73
+
74
+ return str
75
+ end
76
+
77
+ # Calculate the value of f(x) from x
78
+ def calc x
79
+ Error.call "Polynomial::calc: x is not a Numeric value" if !x.is_a?Numeric
80
+
81
+ y = 0
82
+ @coef.each do |coef,value|
83
+ y += value * x**coef
84
+ end
85
+
86
+ return y
87
+ end
88
+ end
@@ -0,0 +1,374 @@
1
+ #encoding: utf-8
2
+
3
+ require_relative 'error.class'
4
+ require_relative 'matrix.class'
5
+
6
+ class Vector
7
+ attr_accessor :x, :y, :z, :verbose
8
+ attr_reader :matrix_op
9
+
10
+ # == Parameters:
11
+ # par1::
12
+ # The first parameter may be a point coordonate x, or a other Vector.
13
+ # If it's a Vector, it will be copied. Else if it is anything else, it will be converted as Float and stored as a abcissa.
14
+ # par2,par3::
15
+ # Theses optionals parameters are used if the first parameter is not a Vector. If it occure, par2 is mandatory (but not par3).
16
+ # They will be stored in @y and @z.
17
+ # verbose::
18
+ # verbose turn on/off the messages with translate, rotate, ...
19
+ #
20
+ # == Returns:
21
+ # himself
22
+ #
23
+ # == Errors::
24
+ # If a parameter is invalid, it may be crash the programm with an ERR_HIGH
25
+ def initialize par1, par2=nil, par3=nil, verbose=true
26
+ if par1.is_a?Vector #and par2 == nil and par3 == nil
27
+ @x = par1.x
28
+ @y = par1.y
29
+ @z = par1.z
30
+
31
+ elsif par1 != nil and par2 != nil
32
+ if par3 == nil
33
+ par3 = 1.0
34
+ end
35
+
36
+ if !Error.isnum?par1 or !Error.isnum?par2 or !Error.isnum?par3
37
+ Error.call "Vector::new : a passed argument is not a valid number"
38
+ end
39
+
40
+ @x = par1.to_f
41
+ @y = par2.to_f
42
+ @z = par3.to_f
43
+ else
44
+ Error.call "Vector::new : The vector couldn't be initialisze with theses parameters : :par1 => '#{par1}', :par2 => '#{par2}'"
45
+ end
46
+
47
+ @verbose = verbose
48
+ @matrix_op = init_matrix_op
49
+ return self
50
+ end
51
+
52
+ # Return to the norm of the currenet vector
53
+ def norm
54
+ Math.sqrt(@x**2 + @y**2 + @z**2)
55
+ end
56
+
57
+ # == Returns:
58
+ # Matrix.new [[1, 0, 0],[0, 1, 0], [0, 0, 1]]
59
+ def init_matrix_op
60
+ @matrix_op = Matrix.new [[1, 0, 0],[0, 1, 0], [0, 0, 1]]
61
+ end
62
+
63
+ # == Parameters:
64
+ # par1::
65
+ # It may be a point coordonate x, or a other Vector.
66
+ # If it's a Vector, it will be used as a couple of coordonates. Else if it is anything else, it will be converted as Float added to @x
67
+ #
68
+ # == Returns:
69
+ # nothing
70
+ #
71
+ # == Errors::
72
+ # If a parameter is invalid, it may be crash the programm with an ERR_HIGH
73
+ # If the vectors do not have the same dimensions, it will display a warning
74
+ def +(par1)
75
+ out = Vector.new(self)
76
+
77
+ if par1 != nil and par1.is_a?Vector #and par2 == nil and par3 == nil
78
+ out.x += par1.x
79
+ out.y += par1.y
80
+
81
+ elsif par1 != nil
82
+ par1 = par1.to_f
83
+ out.x += par1
84
+ out.y += par1
85
+
86
+ if out.z != nil and par1 == nil
87
+ Error.call "The vector #{Vector.new(par1, par1, par1).to_s} do not have the same dimensions than #{out.to_s}", Error::ERR_HIGH
88
+ elsif out.z != nil
89
+ out.z += par1
90
+ end
91
+ else
92
+ Error.call "The vector couldn't be added with this parameters : #{par1}", Error::ERR_HIGH
93
+ end
94
+ return out
95
+ end
96
+
97
+ # == Usage:
98
+ # It is simply like + buf multiply by -1 par1
99
+ def -(par1)
100
+ if par1 == nil
101
+ Error.call "Can't sub nil from vector"
102
+ end
103
+ return (self.+(par1 * -1))
104
+ end
105
+
106
+ # == Parameters:
107
+ # par1:
108
+ # This parameter may be a Vector or a number. If it's a Number, it will multiply all coponents of the Vector.
109
+ # If it's an other vector, we will multiplie their components 2by2
110
+ # == Returns:
111
+ # Vector
112
+ def *(par1)
113
+ out = Vector.new(self)
114
+ if par1.is_a?Numeric
115
+ out.x *= par1
116
+ out.y *= par1
117
+ if out.z != nil
118
+ out.z *= par1
119
+ end
120
+ elsif par1.is_a?Matrix
121
+ return (self.to_matrix * par1).to_vector
122
+ elsif par1.is_a?Vector
123
+ ary1 = [self.x, self.y]
124
+ if out.z != nil
125
+ ary1 << out.z
126
+ end
127
+
128
+ ary2 = [par1.x, par1.y]
129
+ if out.z != nil
130
+ ary2 << par1.z
131
+ end
132
+
133
+ aryr = Matrix.mult_array(ary1, ary2)
134
+ out.x = aryr[0]
135
+ out.y = aryr[1]
136
+ if aryr[2] != nil
137
+ out.z = aryr[2]
138
+ end
139
+ else
140
+ Error.call "Unable to add '#{par1} to this vector", Error::ERR_HIGH
141
+ end
142
+ return out
143
+ end
144
+
145
+ # == Parameters:
146
+ # par1, par2::
147
+ # They are the components of the vector to translate.
148
+ # par3::
149
+ # Optional component (z)
150
+ # see +::
151
+ def translate par1, par2, par3=0.0
152
+ if !Error.isnum? par1 or !Error.isnum? par2 or !Error.isnum? par3
153
+ Error.call "A parameter to the translation is not a valid number", Error::ERR_HIGH
154
+ end
155
+
156
+ s = Matrix.new [[1.0, 0.0, par1.to_f], [0.0, 1.0, par2.to_f], [0.0, 0.0, 1.0]]
157
+ @matrix_op = s
158
+ cpy = self
159
+ cpy.z = 1.0
160
+
161
+ puts "translation de vecteur #{Vector.new(par1,par2,par3).to_s}" if @verbose
162
+
163
+ return (s * cpy.to_matrix).to_vector
164
+ end
165
+
166
+ # == Parameters:
167
+ # c1,c2,c3::
168
+ # c1 and c2 are the coeficiens of the homothetie. c3 is optional
169
+ def homothétie c1, c2, c3=1.0
170
+ if c1 == nil or c2 == nil
171
+ Error.call "Coefficients are invalids"
172
+ end
173
+
174
+ s = Matrix.new [[c1.to_f, 0, 0], [0, c2.to_f, 0], [0, 0, c3.to_f]]
175
+ @matrix_op = s
176
+ cpy = self
177
+ cpy.z = 1.0
178
+
179
+ puts "homothétie de rapports #{c1.to_f}, #{c2.to_f}" if @verbose
180
+
181
+ return (s * cpy.to_matrix).to_vector
182
+ end
183
+
184
+ # == Parameters:
185
+ # a::
186
+ # The angle in degree
187
+ #
188
+ # == Return value:
189
+ # It returns the vector after the translation.
190
+ def rotate a
191
+ if a == nil
192
+ Error.call "Angle is invalid"
193
+ end
194
+
195
+ rad = Math::PI * a.to_f / 180.0
196
+ cpy = self # copy to have the same value in z
197
+ cpy.z = 0.0
198
+ s = Matrix.new [[ Math.cos(rad), -Math.sin(rad), 0], [Math.sin(rad), Math.cos(rad), 0], [0, 0, 1]]
199
+ @matrix_op = s
200
+
201
+ puts "rotation d'angle #{a.to_f}" if @verbose
202
+
203
+ return (s * cpy.to_matrix).to_vector
204
+ end
205
+
206
+ # == Parameters :
207
+ # angle::
208
+ # It is the incline of the line.
209
+ #
210
+ # == Return value:
211
+ # It returns the vector after the translation.
212
+ def symetric angle
213
+ Error.call "Variable angle is not a number (#{angle})", Error::ERR_HIGH if !Error.isnum? angle.to_s
214
+
215
+ rad = Math::PI * angle.to_f / 180.0
216
+ s = Matrix.new [[Math.cos(2 * rad), Math.sin(2 * rad), 0], [Math.sin(2 * rad), -Math.cos(2 * rad), 0], [0, 0, 1]]
217
+ @matrix_op = s
218
+ cpy = self.to_matrix
219
+
220
+ puts "symétrie par rapport à un axe incliné de #{angle.to_f} degrés" if @verbose
221
+
222
+ return (s * cpy).to_vector
223
+ end
224
+
225
+ # == Params:
226
+ # axe::
227
+ # it must be "x" or "y" (case doesn't checked)
228
+ #
229
+ # == Return:
230
+ # The vector after the projection
231
+ def proj_axe axe="x"
232
+ if !axe.match(/[xy]/i)
233
+ Error.call "Vector::proj_axe '#{axe} is not a valid axe", Error::ERR_HIGH
234
+ end
235
+
236
+ s = nil
237
+ if axe.match(/x/i)
238
+ s = Matrix.new [[1, 0, 0], [0, 0, 0], [0, 0, 1]]
239
+ else
240
+ s = Matrix.new [[0, 0, 0], [0, 1, 0], [0, 0, 1]]
241
+ end
242
+
243
+ @matrix_op = s
244
+ cpy = self.to_matrix
245
+
246
+ puts "projection sur un axe #{axe}." if @verbose
247
+
248
+ return (s * cpy).to_vector
249
+ end
250
+
251
+ # == Params:
252
+ # nothing
253
+ # == Return:
254
+ # The vector after the translation
255
+ def symetric_pointo
256
+ return homothétie(-1, -1)
257
+ end
258
+
259
+ # == Parameters:
260
+ # dim::
261
+ # Option option to choose the desired number of dimension of the vector (if is it in 3d, it will be flattened)
262
+ # type::
263
+ # Optional and not used yet. It specify the format of the string. It may only be String yet.
264
+ #
265
+ # == Returns:
266
+ # String
267
+ def to_s dim=3, type=String
268
+ string = ""
269
+ if type == String
270
+ if @y == nil or dim == 1
271
+ string = "(#{@x.to_f.round(3)})"
272
+ elsif @z == nil or dim == 2
273
+ string = "(#{@x.to_f.round(3)}; #{@y.to_f.round(3)})"
274
+ elsif dim == 3
275
+ string = "(#{@x.to_f.round(3)}; #{@y.to_f.round(3)}; #{@z.to_f.round(3)})"
276
+ else
277
+ Error.call "Vector::to_s : Invalid number of dimension specified"
278
+ end
279
+
280
+ else
281
+ Error.call "Vector::to_s : Invalid type conversion", ERR_HIGH
282
+ end
283
+
284
+ return string
285
+ end
286
+
287
+ # == Parameters:
288
+ # type::
289
+ # Optionnal. It specify the format of the array returned. It may be "h" (1) or "w" (0).
290
+ # * If it's "w" or 0, the Array will be [x,y]
291
+ # * If it's "h" or 1, the Array returned will be [[x],[y]]
292
+ #
293
+ # == Returns:
294
+ # Array or an Array of Array
295
+ def to_ary type=0
296
+ array = []
297
+ if type == 0 or type == "w" # everytime true... for the moment
298
+ array << @x << @y
299
+ if @z != nil
300
+ array << @z
301
+ end
302
+ else
303
+ array << [@x] << [@y]
304
+ if @z != nil
305
+ array << [@z]
306
+ end
307
+ end
308
+ return array
309
+ end
310
+
311
+ # == Parameters::
312
+ # Nothing
313
+ #
314
+ # == Returns:
315
+ # Matrix
316
+ def to_matrix
317
+ return Matrix.new self.to_ary(1)
318
+ end
319
+
320
+ # == Parameters:
321
+ # type::
322
+ # It specify the return. It may be String or Array.
323
+ #
324
+ # == Returns:
325
+ # String, Array, nil (see type::)
326
+ #
327
+ # == Errors:
328
+ # nil return occures only if the parameter types:: is not supported.
329
+ def to_a type=String
330
+ if type == String
331
+ return self.to_s
332
+ elsif type == Array
333
+ return self.to_ary
334
+ elsif type == Matrix
335
+ return self.to_matrix
336
+ else
337
+ return nil
338
+ end
339
+ end
340
+
341
+ def nil?
342
+ if @x == 0 and @y == 0 and @z == 0
343
+ true
344
+ else
345
+ false
346
+ end
347
+ end
348
+
349
+ # == Parameters
350
+ # type::
351
+ # Verify if two vectors are collinear
352
+ # == Return:
353
+ # True if vector u and v are collinear else false
354
+ def collinear? v
355
+ Vector::colineaire? @self, v
356
+ end
357
+
358
+ def self.collinear? u, v
359
+ Error.call "Vector::col : invalid parameters" if !u.is_a?Vector or !v.is_a?Vector
360
+ Error.call "Vector::col : vector v1 null", Error::ERR_LOW if u.nil?
361
+ Error.call "Vector::col : vector v2 null", Error::ERR_LOW if v.nil?
362
+
363
+ x = u.y * v.z - u.z * v.y
364
+ y = u.z * v.x - u.x * v.z
365
+ z = u.x * v.y - u.y * v.x
366
+
367
+ if x == 0 and y == 0 and z == 0
368
+ true
369
+ else
370
+ false
371
+ end
372
+ end
373
+
374
+ end
data/lib/epimath100.rb ADDED
@@ -0,0 +1,11 @@
1
+ #encoding: utf-8
2
+
3
+ require_relative "epimath100/error.class.rb"
4
+ require_relative "epimath100/line.class.rb"
5
+ require_relative "epimath100/matrix.class.rb"
6
+ require_relative "epimath100/point.class.rb"
7
+ require_relative "epimath100/polynomial.class.rb"
8
+ require_relative "epimath100/vector.class.rb"
9
+
10
+ class EpiMath100
11
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: epimath100
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.6.0
5
+ platform: ruby
6
+ authors:
7
+ - poulet_a
8
+ - broggi_t
9
+ - bauwen_j
10
+ - caudou_j
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2014-01-18 00:00:00.000000000 Z
15
+ dependencies: []
16
+ description: A simple hello world gem
17
+ email:
18
+ - poulet_a@epitech.eu
19
+ - broggi_t@epitech.eu
20
+ - bauwen_j@epitech.eu
21
+ - caudou_j@epitech.eu
22
+ executables: []
23
+ extensions: []
24
+ extra_rdoc_files: []
25
+ files:
26
+ - lib/epimath100.rb
27
+ - lib/epimath100/error.class.rb
28
+ - lib/epimath100/line.class.rb
29
+ - lib/epimath100/matrix.class.rb
30
+ - lib/epimath100/point.class.rb
31
+ - lib/epimath100/polynomial.class.rb
32
+ - lib/epimath100/vector.class.rb
33
+ homepage: https://github.com/Sophen/epimath100
34
+ licenses:
35
+ - GNU/GPLv3
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.0.14
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: EpiMath100, a ruby gem lib to use lines, functions, points, ...
57
+ test_files: []