Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use hasOwnProperty.call instead of calling the method
In IE8 and below calling `hasOwnProperty` on a DOM element or anything else it doesn't like will blow up, but using `.call` fixes this.
- Loading branch information
Showing
4 changed files
with
302 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,296 @@ | ||
module ExceptionForMatrix | ||
class ErrNotRegular < Exception | ||
def initialize | ||
super("Not Regular Matrix") | ||
end | ||
end | ||
|
||
class ErrDimensionMismatch < Exception | ||
def initialize(name) | ||
super("#{name} dimension mismatch") | ||
end | ||
end | ||
|
||
class ErrOperationNotDefined | ||
def initialize(op, name, on) | ||
super("Operation(#{op}) can't be defined: #{name} op #{on}") | ||
end | ||
end | ||
end | ||
|
||
class Matrix | ||
def self.[](*rows) | ||
rows(rows, false) | ||
end | ||
|
||
def self.rows(rows, copy = true) | ||
end | ||
|
||
def self.columns(columns) | ||
rows(columns, false).transpose | ||
end | ||
|
||
def self.empty(rows = 0, columns = 0) | ||
aise ArgumentError, "One size must be 0" if columns != 0 && rows != 0 | ||
raise ArgumentError, "Negative size" if columns < 0 || row < 0 | ||
|
||
new([[]] * rows, columns) | ||
end | ||
|
||
def self.build(rows, columns = rows, &block) | ||
rows = rows.to_int | ||
columns = columns.to_int | ||
|
||
raise ArgumentError if rows < 0 || columns < 0 | ||
return to_enum :build, rows, columns unless block | ||
|
||
rows = Array.new(row_count) {|i| | ||
Array.new(columns) {|j| | ||
yield i, j | ||
} | ||
} | ||
|
||
new rows, columns | ||
end | ||
|
||
def self.diagonal(*values) | ||
new Array.new(values.length) {|j| | ||
Array.new(values.length, 0).tap {|row| | ||
row[j] = values[j] | ||
} | ||
} | ||
end | ||
|
||
def self.scalar(n, value) | ||
diagonal(*Array.new(n, value)) | ||
end | ||
|
||
def self.identity(n) | ||
scalar(n,1) | ||
end | ||
|
||
def self.zero(rows, columns = rows) | ||
new Array.new(rows) { Array.new(columns, 0) }, columns | ||
end | ||
|
||
def self.row_vector(row) | ||
new [Convert.to_array(row)] | ||
end | ||
|
||
def self.column_vector(column) | ||
new [column].transpose, 1 | ||
end | ||
|
||
def initialize(rows, column_count = rows[-1].size) | ||
@rows = rows | ||
@column_count = columns | ||
end | ||
|
||
def [](i, j) | ||
@rows.fetch(i) { | ||
return nil | ||
}[j] | ||
end | ||
|
||
alias element [] | ||
alias component [] | ||
|
||
def []=(i, j, v) | ||
@rows[i][j] = v | ||
end | ||
|
||
alias set_element []= | ||
alias set_component []= | ||
|
||
def row_count | ||
@rows.length | ||
end | ||
|
||
alias row_size row_count | ||
|
||
attr_reader :column_count | ||
alias column_size column_count | ||
|
||
module Convert | ||
def self.to_array(obj, copy = false) | ||
case obj | ||
when Array | ||
copy ? obj.dup : obj | ||
|
||
when Vector | ||
obj.to_a | ||
|
||
else | ||
obj.to_ary | ||
end | ||
end | ||
end | ||
end | ||
|
||
class Vector | ||
def self.[](*array) | ||
new Matrix::Convert.to_array(array, false) | ||
end | ||
|
||
def self.elements(array, copy = true) | ||
new Matrix::Convert.to_array(array, copy) | ||
end | ||
|
||
def initialize(array) | ||
@elements = array | ||
end | ||
|
||
def [](i) | ||
@elements[i] | ||
end | ||
|
||
alias element [] | ||
alias component [] | ||
|
||
def []=(i, v) | ||
@elements[i] = v | ||
end | ||
|
||
alias set_element []= | ||
alias set_component []= | ||
|
||
def size | ||
@elements.size | ||
end | ||
|
||
def each(&block) | ||
return enum_for :each unless block | ||
|
||
@elements.each(&block) | ||
|
||
self | ||
end | ||
|
||
def along(enum, other, &block) | ||
raise TypeError, "Integer is not like Vector" if Integer === other | ||
|
||
if size != other.size | ||
raise ExceptionForMatrix::ErrDimensionMismatch, :each2, Vector, Vector | ||
end | ||
|
||
|
||
end | ||
|
||
def each2(other, &block) | ||
return enum_for :each2, other unless block | ||
|
||
size.times {|i| | ||
yield @elements[i], other[i] | ||
} | ||
end | ||
|
||
def collect2(other, &block) | ||
raise TypeError, "Integer is not like Vector" if Integer === other | ||
|
||
if size != other.size | ||
raise ExceptionForMatrix::ErrDimensionMismatch, :collect2, Vector, Vector | ||
end | ||
|
||
Array.new(size) {|i| | ||
yield @elements[i], other[i] | ||
} | ||
end | ||
|
||
def ==(other) | ||
return false unless Vector == other | ||
|
||
@elements == other.elements | ||
end | ||
|
||
def eql?(other) | ||
return false unless Vector === other | ||
|
||
@elements.eql? other.elements | ||
end | ||
|
||
def clone | ||
self.class.elements(@elements) | ||
end | ||
|
||
def hash | ||
@elements.hash | ||
end | ||
|
||
def *(other) | ||
case other | ||
when Numeric | ||
self.class.elements(@elements.map(&:*), false) | ||
|
||
when Matrix | ||
Matrix.column_vector(self) * other | ||
|
||
when Vector | ||
raise ExceptionForMatrix::ErrOperationNotDefined, :*, Vector, Vector | ||
end | ||
end | ||
|
||
def +(other) | ||
case other | ||
when Vector | ||
if size != other.size | ||
raise ExceptionForMatrix::ErrDimensionMismatch, :+, Vector, Vector | ||
end | ||
|
||
self.class.elements collect2(other, &:+), false | ||
|
||
when Matrix | ||
Matrix.column_vector(self) + other | ||
end | ||
end | ||
|
||
def -(other) | ||
case other | ||
when Vector | ||
if size != other.size | ||
raise ExceptionForMatrix::ErrDimensionMismatch, :+, Vector, Vector | ||
end | ||
|
||
self.class.elements collect2(other, &:-), false | ||
|
||
when Matrix | ||
Matrix.column_vector(self) - other | ||
end | ||
end | ||
|
||
def /(other) | ||
case other | ||
when Numeric | ||
self.class.elements @elements.collect(&:/), false | ||
|
||
when Matrix, Vector | ||
raise ExceptionForMatrix::ErrOperationNotDefined, :*, Vector, other.class | ||
end | ||
end | ||
|
||
def inner_product(other) | ||
if size != other.size | ||
raise ExceptionForMatrix::ErrDimensionMismatch, :+, Vector, Vector | ||
end | ||
|
||
p = 0 | ||
each2(other) {|v1, v2| | ||
p += v1 * v2.conj | ||
} | ||
p | ||
end | ||
|
||
def collect(&block) | ||
return enum_for :collect unless block | ||
|
||
self.class.elements @elements.collect(&block), false | ||
end | ||
|
||
alias map collect | ||
|
||
def magnitude | ||
`Math.sqrt(#{@elements.reduce(0) { |v, e| `v + Math.pow(Math.abs(e), 2)` }})` | ||
end | ||
|
||
alias r magnitude | ||
alias norm magnitude | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a901ef4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not everyone fits a matrix impl inside an IE8 fix lol
a901ef4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What the fuck. How did this happen I don't know.