Class | Dobjects::Dvector |
In: |
split/Dvector/dvector.c
split/Dvector/lib/Dvector_extras.rb |
Parent: | Object |
now we import the symbols from Dvector
FANCY_READ_DEFAULTS | = | { 'sep' => /\s+/, 'comments' => /^\s*\#/, 'skip_first' => 0, 'index_col' => false, 'headers' => nil, # not used for now. 'default' => 0.0/0.0, # defaults to NaN 'remove_space' => true ,# removes spaces at the beginning of the lines } | Dvector.fancy_read‘s defaults options. See that function for more details | |
WRITE_DEFAULTS | = | { 'sep' => "\t", 'write_mode' => "a", } |
This function is a rudimentary formula computing stuff. Give it a text formula and an array of Dvectors (a), and it returns a Dvector with the result. The formula should contain the following;
column[n]: | represents the current element of the n th Dvector of the array |
This is just a try, and should be implemented in C rather than in Ruby. But if you‘re looking for simplicity, here you go ;-) !
modules are the modules you would wish the evaluator to include. This feature enables one to make sure custom functions are included
Uses Dvectors xs and ys to create a cubic spline interpolant. The xs must be given in ascending order. There is a boundary condition choice to be made for each end concerning the slope. If clamped is true, the correspdoning slope argument value sets the slope. If clamped is false (known as a "free" or "natural" spline), the 2nd derivative is set to 0 and the slope is determined by the fit. In this case, the corresponding slope argument is ignored. The interpolant is an array of Dvectors: [Xs, Ys, As, Bs, Cs]. For x between Xs[j] and Xs[j+1], let dx = x - Xs[j], and find interpolated y for x by y = As[j]*dx^3 + Bs[j]*dx^2 + Cs[j]*dx + Ys[j]. (Spline algorithms derived from Burden & Faires, Numerical Analysis, 4th edition, pp 131 and following.)
This function reads in stream (can an IO object or a String, in which case it represents the name of a file to be opened) the columns specified by cols and returns them. column 0 is the first column. If cols is nil, then fancy_read attempts to find all the columns in the file, while filling absent data with NaN.
opts is a hash for tuning the behavior of the reading. It can hold the following keys:
‘sep’: | the record separator |
‘comments’: | a regular expression matching comment lines |
‘skip_first’: | how many lines to skip at the beginning of the file, |
‘default’: | the value taken for missing elements |
‘index_col’: | if set to true, the first column contains the indices of the corresponding lines (0 for first and so on) |
Checks if the given object is a Dvector. Mainly here for testing purposes, as it corresponds to the internal is_a_dvector.
Returns the maximum value held in the array of Dvectors (or nil if ary is empty). Any nil entries in ary are ignored.
Returns the minimum value held in the array of Dvectors (or nil if ary is empty). Any nil entries in ary are ignored.
Returns a new Dvector. In the first form, the new vector is empty. In the second it is created with size copies of value The third form creates a copy of the other vector passed as a parameter (this can also be an Array). In the last form, a vector of the given size is created. Each element in this vector is calculated by passing the element‘s index to the given block and storing the return value.
Dvector.new -> Dvector[] Dvector.new(2) -> Dvector[ 0, 0 ] Dvector.new(3, -1) -> Dvector[ -1, -1, -1 ] Dvector.new(3) {|i| i**2 + 1} -> Dvector[ 1, 2, 5 ]
The data on the file should be organized in columns of numbers, with one row per line. The start parameter determines the starting line and defaults to 1 meaning start at the first line of the file. The length parameter determines the number of lines to be read and defaults to -1 meaning read to the end of the file. Each column of data is stored in a Dvector. If the dest argument is nil, the result array holds the newly created vectors with the leftmost column in array entry 0, the second column in entry 1, and so on. If dest is not nil, it must be an array with entries either Dvectors or nil. For entries that are Dvectors, the contents of the vector are replaced by the column of data from the file. If the entry is nil, that column of the file is skipped.
The data on the file should be organized in columns of numbers, with one row per line. The start parameter determines the starting line and defaults to 1 meaning start at the first line of the file. The length parameter determines the number of lines to be read and defaults to -1 meaning read to the end of the file. Each column of data is stored in a Dvector. If the dest argument is nil, the result array holds the newly created vectors with the leftmost column in array entry 0, the second column in entry 1, and so on. If dest is not nil, it must be an array with entries either Dvectors or nil. For entries that are Dvectors, the contents of the vector are replaced by the column of data from the file. If the entry is nil, that column of the file is skipped.
This routine reads a row of numbers from the named file. The row argument determines which line of the file to read, starting at 1 for the first line. If the dvector argument is nil, a new Dvector is allocated to hold the row of numbers. Otherwise, the contents of dvector are replaced by the numbers from the line in the file.
The data on the file should be organized in rows of numbers, with one row per line. The rows need not all have the same number of entries since each row is placed in its own Dvector. The start parameter determines the starting line and defaults to 1 meaning start at the first line of the file. The dest must be an array with entries either Dvectors or nil. For entries that are Dvectors, the contents of the vector are replaced by the row of data from the file. The start row is placed in the first entry in dest, the second row in the next, and so on. If the entry in dest is nil, that row of the file is skipped.
Returns the y corresponding to x by spline interpolation using the interpolant which was previously created by calling create_spline_interpolant.
Append—Pushes the given number on to the end of this vector. This expression returns the vector itself, so several appends may be chained together.
Dvector[ 1, 2 ] << -3.3 << 1e3 -> Dvector[ 1, 2, 3.3, 1000.0 ]
Comparison—Returns an integer (-1, 0, or +1) if this vector is less than, equal to, or greater than other. Two vectors are ``equal’’ according to Dvector#<=> if and only if they have the same length and contain exactly the same values.
Dvector[ 1, 1, 2 ] <=> Dvector[ 1, 2, 3 ] -> -1 Dvector[ 1, 1, 2 ] <=> Dvector[ 1, 0, 3 ] -> +1 Dvector[ 1, 1, 2 ] <=> Dvector[ 1, 1, 2, 3 ] -> -1 Dvector[ 1, 2, 3, 4, 5, 6 ] <=> Dvector[ 1, 2 ] -> +1
Element Reference—Returns the element at index int, or returns a subvector starting at start and continuing for length elements, or returns a subvector specified by range. Negative indices count backward from the end of the vector (-1 is the last element). Returns nil if the index (or starting index) are out of range. If the start index equals the vector size and a length or range parameter is given, an empty vector is returned.
a = Dvector[ 1, 2, 3, 4, 5 ] a[2] + a[0] + a[1] -> 6 a[6] -> nil a[1, 2] -> Dvector[ 2, 3 ] a[1..3] -> Dvector[ 2, 3, 4 ] a[4..7] -> Dvector[ 5 ] a[6..10] -> nil a[-3, 3] -> Dvector[ 3, 4, 5 ] # special cases a[5] -> nil a[5, 1] -> Dvector[] a[5..10] -> Dvector[]
Element Assignment—Sets the element at index int, or replaces a subvector starting at start and continuing for length elements, or replaces a subvector specified by range. Returns the assigned object as value. If indices are greater than the current capacity of the vector, the vector grows automatically by adding zeros. Negative indices will count backward from the end of the vector. Inserts elements if length is zero. If nil is used in the second and third forms, deletes elements from dvector. A 1D Array of numbers can be used on the right in the second and third forms in place of a Dvector. An IndexError is raised if a negative index points past the beginning of the vector. See also Dvector#push, and Dvector#unshift.
a = Dvector.new a[4] = 4; -> Dvector[ 0, 0, 0, 0, 4 ] a[0, 3] = [ 1, 2, 3 ] -> Dvector[ 1, 2, 3, 0, 4 ] a[1..2] = [ 1, 2 ] -> Dvector[ 1, 1, 2, 0, 4 ] a[0, 2] = -1 -> Dvector[ -1, 2, 0, 4 ] a[0..2] = 1 -> Dvector[ 1, 4 ] a[-1] = 5 -> Dvector[ 1, 5 ] a[1..-1] = nil -> Dvector[ 1 ]
Called by the marshalling mechanism to store a permanent copy of a Dvector. limit is simply ignored.
Returns of copy of dvector with all entries replaced by their absolute values.
a = Dvector[ 1, -2, -3, 4 ] a.abs -> Dvector[ 1, 2, 3, 4 ]
Returns of copy of dvector with entry x replaced by acos(x).
a = Dvector[ 0.1, -0.2, -0.3, 0.4 ] a.acos -> Dvector[ acos(0.1), acos(-0.2), acos(-0.3), acos(0.4) ]
Returns of copy of dvector with entry x replaced by acosh(x).
a = Dvector[ 5.1, 2.2, 1.3 ] a.acosh -> Dvector[ acosh(5.1), acosh(2.2), acosh(1.3) ]
When argument is a number, this operation returns a copy of dvector with each entry x replaced by x + number. When argument is a vector, this operation returns a copy of dvector with each entry x replaced by x + the corresponding entry in the other vector.
a = Dvector[ 11, -5, 2 ] a.add(3) -> Dvector[ 14, -2, 5 ] a + 3 -> Dvector[ 14, -2, 5 ] 3 + a -> Dvector[ 14, -2, 5 ] b = Dvector[ 7, 4, -10 ] a.add(b) -> Dvector[ 18, -1, -8 ] a + b -> Dvector[ 18, -1, -8 ]
When argument is a number, each entry x in dvector is replaced by x + number. When argument is a vector, each entry x in dvector is replaced by x + the corresponding entry in the other vector.
a = Dvector[ 11, -5, 2 ] a.add!(3) -> Dvector[ 14, -2, 5 ] a -> Dvector[ 14, -2, 5 ] a = Dvector[ 11, -5, 2 ] b = Dvector[ 7, 4, -10 ] a.add!(b) -> Dvector[ 18, -1, -8 ] a -> Dvector[ 18, -1, -8 ]
When argument is a number, this operation returns a copy of dvector with each entry x replaced by number ** x. When argument is a vector, this operation returns a copy of dvector with each entry x replaced by the corresponding entry in the other vector raised to the power x.
a = Dvector[ 2, -5, 12 ] a.as_exponent_of(3.8) -> Dvector[ 3.8 ** 2, 3.8 ** (-5), 3.8 ** 12 ] b = Dvector[ 7.1, 4.9, -10 ] a.as_exponent_of(b) -> Dvector[ 7.1 ** 2, 4.9 ** (-5), (-10) ** 12 ]
When argument is a number, this operation replaces each entry x of dvector by number ** x. When argument is a vector, this operation replaces each entry x of dvector by the corresponding entry in the other vector raised to the power x.
a = Dvector[ 2, -5, 12 ] a.as_exponent_of!(3.8) -> Dvector[ 3.8 ** 2, 3.8 ** (-5), 3.8 ** 12 ] a -> Dvector[ 3.8 ** 2, 3.8 ** (-5), 3.8 ** 12 ] b = Dvector[ 7.1, 4.9, -10 ] a.as_exponent_of!(b) -> Dvector[ 7.1 ** 2, 4.9 ** (-5), (-10) ** 12 ] a -> Dvector[ 7.1 ** 2, 4.9 ** (-5), (-10) ** 12 ]
Returns of copy of dvector with entry x replaced by asin(x).
a = Dvector[ 0.1, -0.2, -0.3, 0.4 ] a.asin -> Dvector[ asin(0.1), asin(-0.2), asin(-0.3), asin(0.4) ]
Returns of copy of dvector with entry x replaced by asinh(x).
a = Dvector[ 0.1, -0.2, 0.3 ] a.asinh -> Dvector[ asinh(0.1), asinh(-0.2), asinh(0.3) ]
Returns of copy of dvector with entry x replaced by atan(x).
a = Dvector[ 0.1, -0.2, -0.3, 0.4 ] a.atan -> Dvector[ atan(0.1), atan(-0.2), atan(-0.3), atan(0.4) ]
When argument is a number, this operation returns a copy of dvector with each entry x replaced by the angle whose tangent is x/number. When argument is a vector, this operation returns a copy of dvector with each entry x replaced by the angle whose tangent is x divided by the corresponding entry in the other vector.
a = Dvector[ 1.1, -5.7, 12.7 ] a.atan2(3.8) -> Dvector[ atan2(1.1, 3.8), atan2(-5.7,3.8), atan2(12.7,3.8) ] b = Dvector[ 7.1, 4.9, -10.1 ] a.atan2(b) -> Dvector[ atan2(1.1,7.1), atan2(-5.7,4.9), atan2(12.7,-10.1) ]
When argument is a number, this operation replaces each entry x of dvector by the angle whose tangent is x/number. When argument is a vector, this operation replaces each entry x of dvector by the angle whose tangent is x divided by the corresponding entry in the other vector.
a = Dvector[ 1.1, -5.7, 12.7 ] a.atan2!(3.8) -> Dvector[ atan2(1.1, 3.8), atan2(-5.7,3.8), atan2(12.7,3.8) ] a = Dvector[ 1.1, -5.7, 12.7 ] b = Dvector[ 7.1, 4.9, -10.1 ] a.atan2!(b) -> Dvector[ atan2(1.1,7.1), atan2(-5.7,4.9), atan2(12.7,-10.1) ]
Returns of copy of dvector with entry x replaced by atanh(x).
a = Dvector[ 0.1, -0.2, 0.3 ] a.atanh -> Dvector[ atanh(0.1), atanh(-0.2), atanh(0.3) ]
Returns of copy of dvector with entry x replaced by smallest integer not less than x.
a = Dvector[ 1.1, -2.2, 5.3 ] a.ceil -> Dvector[ 2, -2, 6 ]
Replace each entry x of dvector with the smallest integer not less than x.
a = Dvector[ 1.1, -2.2, 5.3 ] a.ceil! -> Dvector[ 2, -2, 6 ] a -> Dvector[ 2, -2, 6 ]
Invokes block once for each element of dvector. Returns a new vector holding the values returned by block. Note that for numeric operations on long vectors, it is more efficient to apply the operator directly to the vector rather than using map or collect.
a = Dvector[ 1, 2, 3, 4 ] a.map {|x| x**2 + 1 } -> Dvector[ 2, 5, 10, 17 ] A better way: a = Dvector[ 1, 2, 3, 4 ] a**2 + 1 -> Dvector[ 2, 5, 10, 17 ]
Invokes block once for each element of dvector, replacing the element with the value returned by block. Note that for numeric operations on long vectors, it is more efficient to apply the operator directly to the vector rather than using these operators.
a = Dvector[ 2, -3, 7 ] a.map! {|x| x**2 + 1 } -> Dvector[ 5, 10, 50 ] a -> Dvector[ 5, 10, 50 ]
A better way:
a.mul!(a).add!(1) -> Dvector[ 5, 10, 50 ]
Invokes block once for each element of dvector, replacing the element with the value returned by block.
a = Dvector[ 1, 0, -1 ] b = Dvector[ 3, 4, 5 ] a.map2!(b) {|x,y| x**2 + y**2 } -> Dvector[ 10, 16, 26 ] a -> Dvector[ 10, 16, 26 ]
Returns of copy of dvector with entry x replaced by cos(x).
a = Dvector[ 1, -2, -3, 4 ] a.cos -> Dvector[ cos(1), cos(-2), cos(-3), cos(4) ]
Returns of copy of dvector with entry x replaced by cosh(x).
a = Dvector[ 0.1, -0.2, 0.3 ] a.cosh -> Dvector[ cosh(0.1), cosh(-0.2), cosh(0.3) ]
Deletes items from dvector that are equal to number. If number is not found, returns nil. If the optional code block is given, returns the result of block if the item is not found.
a = Dvector.new(5) {|i| i*3 } a.delete(6) -> 6 a -> Dvector[0, 3, 9, 12] a.delete(2) -> nil a.delete(2) { "not found" } -> "not found"
Deletes every element of dvector for which block evaluates to true.
a = Dvector[ 1, 2, 3, 4 ] a.delete_if {|x| x.modulo(2) == 0 } -> Dvector[1, 3] a -> Dvector[ 1, 3 ]
When argument is a number, this operation returns a copy of dvector with each entry x replaced by x / number. When argument is a vector, this operation returns a copy of dvector with each entry x replaced by x / the corresponding entry in the other vector.
a = Dvector[ 1.1, -5.7, 2.5 ] a.div(3.8) -> Dvector[ 1.1/3.8, -5.7/3.8, 2.5/3.8 ] a / 3.8 -> Dvector[ 1.1/3.8, -5.7/3.8, 2.5/3.8 ] 3 / a -> Dvector[ 3.8/1.1, -3.8/5.7, 3.8/2.5 ] b = Dvector[ 7.1, 4.9, -10.1 ] a.div(b) -> Dvector[ 1.1/7.1, -5.7/4.9, 2.5/10.1 ] a / b -> Dvector[ 1.1/7.1, -5.7/4.9, 2.5/10.1 ]
When argument is a number, each entry x in dvector is replaced by x / number. When argument is a vector, each entry x in dvector is replaced by x / the corresponding entry in the other vector.
a = Dvector[ 1.1, -5.7, 2.5 ] a.div!(3.8) -> Dvector[ 1.1/3.8, -5.7/3.8, 2.5/3.8 ] a -> Dvector[ 1.1/3.8, -5.7/3.8, 2.5/3.8 ] a = Dvector[ 1.1, -5.7, 2.5 ] b = Dvector[ 7.1, 4.9, -10.1 ] a.div!(b) -> Dvector[ 1.1/7.1, -5.7/4.9, 2.5/10.1 ] a -> Dvector[ 1.1/7.1, -5.7/4.9, 2.5/10.1 ]
Calls block once for each element in dvector, passing that element as a parameter.
a = Dvector[ 1, 0, -1 ] a.each {|x| print x, " -- " }
produces:
1 -- 0 -- -1 --
Calls block once for each element in dvector, passing that element as a parameter along with the corresponding element from the other vector. The two vectors must be the same length.
a = Dvector[ 1, 0, -1 ] b = Dvector[ 3, 4, 5 ] a.each2(b) {|x,y| print "(", x ",", y, ") " }
produces:
(1,3) (0,4) (-1,5)
Same as Dvector#each, but passes the index of the element instead of the element itself.
a = Dvector[ 1, 0, -1 ] a.each_index {|x| print x, " -- " }
produces:
0 -- 1 -- 2 --
Same as Dvector#each, but passes the index of the element in addition to the element itself.
Returns true if dvector and other have the same content. other can either be a Dvector or a 1D Array of numbers.
Returns of copy of dvector with each entry x replaced by 10**x.
a = Dvector[ 1.1, -2.2, 5.3 ] a.exp10 -> Dvector[ 10**(1.1), 10**(-2.2), 10**(5.3) ]
Replace each entry x of dvector with 10**x.
a = Dvector[ 1.1, -2.2, 5.3 ] a.exp10! -> Dvector[ 10**(1.1), 10**(-2.2), 10**(5.3) ] a -> Dvector[ 10**(1.1), 10**(-2.2), 10**(5.3) ]
Tries to return the element at index int. If the index lies outside the vector, the first form throws an IndexError exception, the second form returns default, and the third form returns the value of invoking the block, passing in the index. Negative values of the index count from the end of the vector.
a = Dvector[ 11, 22, 33, 44 ] a.fetch(1) -> 22 a.fetch(-1) -> 44 a.fetch(4, 0) -> 0 a.fetch(4) { |i| i*i } -> 16
The first three forms set the selected elements of dvector (which may be the entire vector) to number. A start of nil is equivalent to zero. A length of nil is equivalent to dvector.length. The last three forms fill the vector with the value of the block. The block is passed the absolute index of each element to be filled.
a = Dvector[ 1, 2, 3, 4, 5 ] a.fill(-1) -> Dvector[ -1, -1, -1, -1, -1 ] a.fill(7, 2, 2) -> Dvector[ -1, -1, 7, 7, -1 ] a.fill(8, 0..1) -> Dvector[ 8, 8, 7, 7, -1 ] a.fill {|i| i*i} -> Dvector[ 0, 1, 4, 9, 16 ] a.fill(-2) {|i| i*i*i} -> Dvector[ 0, 1, 4, 27, 64 ] a -> Dvector[ 0, 1, 4, 27, 64 ]
Returns of copy of dvector with each entry x replaced by largest integer not greater than x.
a = Dvector[ 1.1, -2.2, 5.3 ] a.floor -> Dvector[ 1, -3, 5 ]
Replace each entry x of dvector with the largest integer not greater than x.
a = Dvector[ 1.1, -2.2, 5.3 ] a.floor! -> Dvector[ 1, -3, 5 ] a -> Dvector[ 1, -3, 5 ]
Returns true if number is present in dvector, false otherwise.
a = Dvector[ 1, 2, 3 ] a.include?(2) -> true a.include?(0) -> false
Replaces the contents of dvector with the contents of other, truncating or expanding if necessary.
a = Dvector[ 1, 2, 3, 4, 5 ] a.replace(Dvector[ -1, -2, -3 ]) -> Dvector[ -1, -2, -3 ] a -> Dvector[ -1, -2, -3 ]
If the index is not negative, insert the given values before
the element with the index _int_. If the index is -1, appends the values to _dvector_. Otherwise inserts the values after the element with the given index. a = Dvector[ 1, 2, 3 ] a.insert(2, 99) -> Dvector[ 1, 2, 99, 3 ] a.insert(-2, 1, 2, 3) -> Dvector[ 1, 2, 99, 1, 2, 3, 3 ] a.insert(-1, 0) -> Dvector[ 1, 2, 99, 1, 2, 3, 3, 0 ]
Returns of copy of dvector with each entry x replaced by 1/x.
a = Dvector[ 1.1, -2.2, 5.3 ] a.inv -> Dvector[ 1 / 1.1, -1 / 2.2, 1 / 5.3 ]
Replace each entry x of dvector with 1/x.
a = Dvector[ 1.1, -2.2, 5.3 ] a.inv! -> Dvector[ 1 / 1.1, -1 / 2.2, 1 / 5.3 ] a -> Dvector[ 1 / 1.1, -1 / 2.2, 1 / 5.3 ]
Returns a string created by converting each element of the vector to a string, separated by sep.
Dvector[ 1, 2, 3 ].join -> "1 2 3" Dvector[ 1, 2, 3 ].join("-") -> "1-2-3"
Returns the number of elements in dvector.
Dvector[ 0, -1, 2, -3, 4 ].length -> 5 Dvector[].length -> 0
Replaces contents of dvector by control points for Bezier curve. The cubic, y(x), is defined from x0 to x0+delta_x. At location x = x0 + dx, with dx between 0 and delta_x, define y = a*dx^3 + b*dx^2 + c*dx + y0. This routine replaces the contents of dest by [x1, y1, x2, y2, x3, y3], the Bezier control points to match this cubic.
First form returns the entry with the maximum value in dvector, nil if dvector is empty. Second form returns maximum of all the vectors (or nil if all are empty).
a = Dvector[ 1, 2, 3, 4, 5, 4, 3, 5, 2 ] a.max -> 5 Dvector[].max -> nil b = Dvector[ 8, 3, 0, 7 ] a.max(b) -> 8
First form returns the entry with the minimum value in dvector, nil if dvector is empty. Second form returns minimum of all the vectors (or nil if all are empty).
a = Dvector[ 1, 2, 3, 4, 5, 4, 3, 5, 2 ] a.min -> 1 Dvector[].min -> nil b = Dvector[ 8, 3, 0, 7 ] a.min(b) -> 0
When argument is a number, this operation returns a copy of dvector with each entry x replaced by x % number. When argument is a vector, this operation returns a copy of dvector with each entry x replaced by x % the corresponding entry in the other vector.
a = Dvector[ 1.1, -5.7, 12.7 ] a.mod(3.8) -> Dvector[ 1.1, 1.9, 1.3 ] a % 3.8 -> Dvector[ 1.1, 1.9, 1.3 ] b = Dvector[ 7.1, 4.9, -10.1 ] a.mod(b) -> Dvector[ 1.1, 4.1, -7.5 ] a % b -> Dvector[ 1.1, 4.1, -7.5 ]
When argument is a number, this operation returns a copy of dvector with each entry x replaced by x % number. When argument is a vector, this operation returns a copy of dvector with each entry x replaced by x % the corresponding entry in the other vector.
a = Dvector[ 1.1, -5.7, 12.7 ] a.mod!(3.8) -> Dvector[ 1.1, 1.9, 1.3 ] a -> Dvector[ 1.1, 1.9, 1.3 ] b = Dvector[ 7.1, 4.9, -10.1 ] a.mod!(b) -> Dvector[ 1.1, 4.1, -7.5 ] a -> Dvector[ 1.1, 4.1, -7.5 ]
When argument is a number, this operation returns a copy of dvector with each entry x replaced by x * number. When argument is a vector, this operation returns a copy of dvector with each entry x replaced by x * the corresponding entry in the other vector.
a = Dvector[ 11, -5, 2 ] a.mul(3) -> Dvector[ 33, -15, 6 ] a * 3 -> Dvector[ 33, -15, 6 ] 3 * a -> Dvector[ 33, -15, 6 ] b = Dvector[ 7, 4, -10 ] a.mul(b) -> Dvector[ 77, -20, -20 ] a * b -> Dvector[ 77, -20, -20 ]
When argument is a number, each entry x in dvector is replaced by x * number. When argument is a vector, each entry x in dvector is replaced by x * the corresponding entry in the other vector.
a = Dvector[ 11, -5, 2 ] a.mul!(3) -> Dvector[ 33, -15, 6 ] a -> Dvector[ 33, -15, 6 ] a = Dvector[ 11, -5, 2 ] b = Dvector[ 7, 4, -10 ] a.mul!(b) -> Dvector[ 77, -20, -20 ] a -> Dvector[ 77, -20, -20 ]
Returns of copy of dvector with each entry x replaced by -x.
a = Dvector[ 1, 2, 3, 4 ] a.neg -> Dvector[ -1, -2, -3, -4 ]
Replace each entry x of dvector with -x.
a = Dvector[ 1.1, -2.2, 5.3 ] a.neg! -> Dvector[ -1.1, 2.2, -5.3 ] a -> Dvector[ -1.1, 2.2, -5.3 ]
Removes the last element from dvector and returns it, or nil if the vector is empty.
a = Dvector[ 1, 2, 3 ] a.pop -> 3 a -> Dvector[ 1, 2 ]
When argument is a number, this operation returns a copy of dvector with each entry x replaced by x ** number. When argument is a vector, this operation returns a copy of dvector with each entry x replaced by x ** the corresponding entry in the other vector.
a = Dvector[ 1.1, -5.7, 12.7 ] a.raised_to(3) -> Dvector[ 1.1 ** 3, (-5.7) ** 3, 12.7 ** 3 ] a ** 3 -> Dvector[ 1.1 ** 3, (-5.7) ** 3, 12.7 ** 3 ] b = Dvector[ 7, 4, -2 ] a.raised_to(b) -> Dvector[ 1.1 ** 7, (-5.7) ** 4, 12.7 ** (-2) ] a ** b -> Dvector[ 1.1 ** 7, (-5.7) ** 4, 12.7 ** (-2) ]
When argument is a number, this operation returns a copy of dvector with each entry x replaced by x ** number. When argument is a vector, this operation returns a copy of dvector with each entry x replaced by x ** the corresponding entry in the other vector.
a = Dvector[ 1.1, -5.7, 12.7 ] a.raised_to!(3) -> Dvector[ 1.1 ** 3, (-5.7) ** 3, 12.7 ** 3 ] a -> Dvector[ 1.1 ** 3, (-5.7) ** 3, 12.7 ** 3 ] b = Dvector[ 7, 4, -2 ] a.raised_to!(b) -> Dvector[ 1.1 ** 7, (-5.7) ** 4, 12.7 ** (-2) ] a -> Dvector[ 1.1 ** 7, (-5.7) ** 4, 12.7 ** (-2) ]
Append—Pushes the given number(s) on to the end of this vector.
a = Dvector[ 1, 2, 3 ] a.push(4, 5, 6) -> Dvector[1, 2, 3, 4, 5, 6]
Returns a new vector containing the items in dvector, except those for which the block is true.
a = Dvector[ 1, 2, 3, 4 ] a.reject {|x| x.modulo(2) == 0 } -> Dvector[1, 3]
Equivalent to Dvector#delete_if, deleting elements from dvector for which the block evaluates to true, but returns nil if no changes were made. Also see Enumerable#reject.
When the argument is a number, this operation returns a copy of dvector with each entry x replaced by the remainder of x divided by number. When the argument is a vector, this operation returns a copy of dvector with each entry x replaced by the remainder of x divided by the corresponding entry in the other vector.
a = Dvector[ 11, -5, 2 ] a.remainder(3) -> Dvector[ 2, -2, 2 ] b = Dvector[ 2, 3, 5 ] a.remainder(b) -> Dvector[ 1, -2, 2 ]
When the argument is a number, this operation replaces with each entry x of dvector by the remainder of x divided by number. When the argument is a vector, this operation replaces with each entry x of dvector by remainder of x divided by the corresponding entry in the other vector.
a = Dvector[ 11, -5, 2 ] a.remainder!(3) -> Dvector[ 2, -2, 2 ] a -> Dvector[ 2, -2, 2 ] a = Dvector[ 11, -5, 2 ] b = Dvector[ 2, 3, 5 ] a.remainder!(b) -> Dvector[ 1, -2, 2 ] a -> Dvector[ 1, -2, 2 ]
Replaces the contents of dvector with the contents of other, truncating or expanding if necessary.
a = Dvector[ 1, 2, 3, 4, 5 ] a.replace(Dvector[ -1, -2, -3 ]) -> Dvector[ -1, -2, -3 ] a -> Dvector[ -1, -2, -3 ]
Reverses dvector in place.
a = Dvector[ 1, 2, 3 ] a.reverse! -> Dvector[ 3, 2, 1 ] a -> Dvector[ 3, 2, 1 ]
Same as Dvector#each, but traverses dvector in reverse order.
a = Dvector[ 1, 0, -1 ] a.each {|x| print x, " " }
produces:
-1 0 1
Same as Dvector#each2, but traverses vectors in reverse order. The vectors must have the same size.
a = Dvector[ 1, 0, -1 ] b = Dvector[ 3, 4, 5 ] a.reverse_each2(b) {|x,y| print "(", x ",", y, ") " }
produces:
(-1,5) (0,4) (1,3)
Same as Dvector#each2_with_index, but traverses the vectors in reverse order.
a = Dvector[ 1, 0, -1 ] b = Dvector[ 3, 4, 5 ] a.reverse_each2_with_index(b) {|x,y,i| print "(", x ",", y, "," i, ") " }
produces:
(-1,5,2) (0,4,1) (1,3,0)
Same as Dvector#reverse_each, but passes the index of the element instead of the element itself.
a = Dvector[ 1, 0, -1 ] a.reverse_each_index {|i| print i, " -- " }
produces:
2 -- 1 -- 0 --
Same as Dvector#each_with_index, but traverses dvector in reverse order.
a = Dvector[ 1, 0, -1 ] a.reverse_each_with_index {|x,i| print "(", x, ",", i, ") " }
produces:
(-1,2) (0,1) (1,0)
Invokes the block passing in successive elements from dvector, returning a vector containing those elements for which the block returns a true value (equivalent to Enumerable#select).
a = Dvector[ 1, 2, 3, 4, 5, 6 ] a.select {|x| x.modulo(2) == 0 } -> Dvector[2, 4, 6]
Returns the first element of dvector and removes it (shifting all other elements down by one). Returns nil if the vector is empty.
args = Dvector[ 1, 2, 3 ] args.shift -> 1 args -> Dvector[ 2, 3 ]
Returns of copy of dvector with entry x replaced by sin(x).
a = Dvector[ 1, -2, -3, 4 ] a.sin -> Dvector[ sin(1), sin(-2), sin(-3), sin(4) ]
Returns of copy of dvector with entry x replaced by sinh(x).
a = Dvector[ 0.1, -0.2, 0.3 ] a.sinh -> Dvector[ sinh(0.1), sinh(-0.2), sinh(0.3) ]
Deletes the element(s) given by an index (optionally with a length) or by a range. Returns the deleted object, subvector, or nil if the index is out of range.
a = Dvector.new(5) {|i| i*3 } a.slice!(1) -> 3 a -> Dvector[0, 6, 9, 12] a.slice!(-1) -> 12 a -> Dvector[0, 6, 9] a.slice!(100) -> nil a -> Dvector[0, 6, 9] a.slice!(1..2) -> Dvector[6, 9] a -> Dvector[0] a.slice!(1..2) -> Dvector[] a -> Dvector[0] a.slice!(0..2) -> Dvector[0] a -> Dvector[]
Returns a new vector created by sorting dvector. Comparisons for the sort will be done using the <=> operator or using an optional code block. The block implements a comparison between a and b, returning -1, 0, or +1.
a = Dvector[ 4, 1, 2, 5, 3 ] a.sort -> Dvector[ 1, 2, 3, 4, 5 ] a -> Dvector[ 4, 1, 2, 5, 3 ] a.sort {|x,y| y <=> x } -> Dvector[ 5, 4, 3, 2, 1 ]
Sorts dvector in place. dvev is effectively frozen while a sort is in progress. Comparisons for the sort will be done using the <=> operator or using an optional code block. The block implements a comparison between a and b, returning -1, 0, or +1.
a = Dvector[ 4, 1, 2, 5, 3 ] a.sort! -> Dvector[ 1, 2, 3, 4, 5 ] a -> Dvector[ 1, 2, 3, 4, 5 ] a.sort! {|x,y| y <=> x } -> Dvector[ 5, 4, 3, 2, 1 ] a -> Dvector[ 5, 4, 3, 2, 1 ]
When argument is a number, this operation returns a copy of dvector with each entry x replaced by x - number. When argument is a vector, this operation returns a copy of dvector with each entry x replaced by x - the corresponding entry in the other vector.
a = Dvector[ 11, -5, 2 ] a.sub(3) -> Dvector[ 8, -8, -1 ] a - 3 -> Dvector[ 8, -8, -1 ] 3 - a -> Dvector[ -8, 8, 1 ] b = Dvector[ 7, 4, -10 ] a.sub(b) -> Dvector[ 4, -9, 12 ] a - b -> Dvector[ 4, -9, 12 ]
When argument is a number, each entry x in dvector is replaced by x - number. When argument is a vector, each entry x in dvector is replaced by x - the corresponding entry in the other vector.
a = Dvector[ 11, -5, 2 ] a.sub!(3) -> Dvector[ 8, -8, -1 ] a -> Dvector[ 8, -8, -1 ] b = Dvector[ 7, 4, -10 ] a = Dvector[ 11, -5, 2 ] a.sub!(b) -> Dvector[ 4, -9, 12 ] a -> Dvector[ 4, -9, 12 ]
Returns the sum of the entries in dvector. Returns 0.0 if dvector is empty.
a = Dvector[ 1, 2, 3, 4 ] a.sum -> 10 Dvector[].sum -> 0
Returns of copy of dvector with entry x replaced by tan(x).
a = Dvector[ 1, -2, -3, 4 ] a.tan -> Dvector[ tan(1), tan(-2), tan(-3), tan(4) ]
Returns of copy of dvector with entry x replaced by tanh(x).
a = Dvector[ 0.1, -0.2, 0.3 ] a.tanh -> Dvector[ tanh(0.1), tanh(-0.2), tanh(0.3) ]
Sets contents of dvector to solution vector u of the following tri-diagonal matrix problem.
| b[0] c[0] 0 ... | | u[0] | | r[0] | | a[1] b[1] c[1] ... | | u[1] | | r[1] | | ... | * | ... | = | ... | | ... a[n-2] b[n-2] c[n-2] | | u[n-2] | | r[n-2] | | ... 0 a[n-1] b[n-1] | | u[n-1] | | r[n-1] |
This corresponds to solving difference equations of the form
a[j] * u[j-1] + b[j] * u[j] + c[j] * u[j+1] = r[j], for 0 < j < n,
with boundary conditions
u[0] = (r[0] - c[0] * u[1]) / b[0], and u[n-1] = (r[n-1] - a[n-1] * u[n-2]) / b[n-1].
See Numerical Recipes for more details.
Returns a copy of dvector with any entry with absolute value less than cutoff replaced by 0.
a = Dvector[ 1.1, 1e-20, -5.3 ] a.trim -> Dvector[ 1.1, 0, -5.3 ]
Each entry x in dvector having absolute value less than cutoff is replaced by 0.
a = Dvector[ 1.1, 1e-20, -5.3 ] a.trim! -> Dvector[ 1.1, 0, -5.3 ]
Returns a new vector by removing duplicate elements from dvector. Remove the element if there is a later one in the vector that is equal to it.
a = Dvector[ 1.1, 3.8, 1.7, 3.8, 5 ] a.uniq -> Dvector[1.1, 1.7, 3.8, 5]
Same as Dvector#uniq, but modifies the receiver in place. Returns nil if no changes are made (that is, no duplicates are found).
Removes duplicate elements from dvector. Deletes the element if there is a later one in the vector that is equal to it. a = Dvector[ 1.1, 3.8, 1.7, 3.8, 5 ] a.uniq! -> Dvector[1.1, 1.7, 3.8, 5] b = Dvector[ 1.1, 3.8, 1.7, 5 ] b.uniq! -> nil
Prepends objects to the front of dvector, moving other elements up one.
a = [ 2, 3, 4 ] a.unshift(1) -> Dvector[ 1, 2, 3, 4 ] a.unshift(-1, 0) -> Dvector[ -1, 0, 1, 2, 3, 4 ]
Returns a new vector containing the elements in dvector corresponding to the given selector(s). The selectors may be either integer indices or ranges.
a = Dvector[ 1, 2, 3, 4, 5, 6 ] a.values_at(1, 3, 5) -> Dvector[ 2, 4, 6 ] a.values_at(1, 3, 5, 7) -> Dvector[ 2, 4, 6 ] a.values_at(-1, -3, -5, -7) -> Dvector[ 6, 4, 2 ] a.values_at(1..3, 2...5) -> Dvector[ 2, 3, 4, 3, 4, 5 ]
Returns square root of the dot product of the vector with itself.
a = Dvector[ 3, 5 ] a.vector_length -> 5.0