Flash Math

Build Status Coverage Status Code Climate

Flash Validators is a collection of advance math modules.

Currently supported calculations: geometry and statistics.

Installation

Add this line to your application's Gemfile:

gem 'flash_math'

And then execute:

$ bundle

Or install it yourself as:

$ gem install flash_math

Usage

Geometry

Geometrical Point:

Use the following methods to generate geometical point: GeometricPoint.new(x, y) and GeometricPoint.new_by_array(array)

Use the following methods to generate geometical point data: advance_by(vector) and to_vector

geo_point = GeometricPoint.new(1, 2)
geo_vector = GeometricVector.new(2, -1)

geo_point.x #=> 1
geo_point.2 #=> 2
geo_point.advance_by(geo_vector) #=> [3, 1]

Geometrical Vector:

Use the following methods to generate geometical vector: GeometricVector.new(x, y) and GeometricVector.new_by_array(array)

Use the following methods to generate geometical vector data: modulus, collinear_with?(vector), cross_product(vector), and scalar_product(vector)

geo_vector1 = GeometricVector.new(1, 2)
geo_vector2 = GeometricVector.new(2, 4)

geo_vector1.x #=> 1
geo_vector1.y #=> 2
geo_vector1.modulus #=> 2.23606797749979
geo_vector1.collinear_with?(geo_vector2) #=> true
geo_vector1.cross_product(geo_vector2) #=> 10
geo_vector1.scalar_product(geo_vector2) #=> 0

Geometrical Distance:

Use the following method to generate geometical distance: GeometricDistance.new(x, y) and GeometricDistance.new_by_arrays(array)

Use the following methods to generate geometical distance data: distance, midpoint, and midpoint_distance

geo_distance = GeometricalDistance.new_by_array([1, 1], [2, 4])

geo_distance.distance #=> 1
geo_distance.midpoint_distance #=> 0.5
geo_distance.midpoint #=> [2, 3]

Geometrical Line:

Use the following method to generate geometical line: GeometricLine.new(point1, point2) and GeometricLine.new_by_arrays(point1, point2)

Use the following methods to generate geometical line data: angel_to, distance_to, horizontal?, vertical?, intersect_x, parallel_to?, slope, x_intercept, and y_intercept

geo_line1 = GeometricLine.new_by_arrays([0, 0], [1, 1])
geo_line2 = GeometricLine.new_by_arrays([0, 0], [1, -1])
point = GeometricPoint.new(3, 4)

geo_line1.angle_to(geo_line2) #=> 1.5707963267948966
geo_line1.distance_to(point) #=> 0.7071067811865475
geo_line1.horizontal? #=> false
geo_line1.vertical? #=> false
geo_line1.intersect_x(geo_line2) #=> 0.0
geo_line1.parallel_to?(geo_line2) #=> false
geo_line1.slope #=> 1.0
geo_line1.x_intercept #=> 0.0
geo_line1.y_intercept #=> 0.0

Geometrical Segment:

Use the following method to generate geometical segment: GeometricSegment.new(point1, point2) and GeometricSegment.new_by_arrays(point1, point2)

Use the following methods to generate geometical segment data: bottommost_endpoint, leftmost_endpoint, rightmost_endpoint, topmost_endpoint, contains_point?, distance_to?, intersection_point_with, intersects_with?, length, lies_on_one_line_with?, overlaps?, parallel_to?, and to_vector

geo_segment1 = GeometricSegment.new_by_arrays([0, 0], [1, 1])
geo_segment2 = GeometricSegment.new_by_arrays([2, 2], [3, 3])
point = GeometricPoint.new(0, 1)

geo_segment1.bottommost_endpoint #=> [0, 0]
geo_segment1.leftmost_endpoint #=> [0, 0]
geo_segment1.rightmost_endpoint #=> [1, 1]
geo_segment1.topmost_endpoint #=> [1, 1]
geo_segment1.contains_point?(point) #=> true
geo_segment1.distance_to?(point) #=> 0.7071067811865476
geo_segment1.intersection_point_with(geo_segment2) #=> GeometricSegmentsDoNotIntersect error
geo_segment1.intersects_with?(geo_segment2) #=> false
geo_segment1.length #=> 1.4142135623730951
geo_segment1.lies_on_one_line_with?(geo_segment2) #=> true
geo_segment1.overlaps?(geo_segment2) #=> false
geo_segment1.parallel_to?(geo_segment2) #=> true
geo_segment1.to_vector #=> [0, 0]

Geometrical Bounding Box:

Use the following method to generate geometical bounding box: GeometricBoundingBox.new(point1, point2) and GeometricBoundingBox.new_by_arrays(point1, point2)

Use the following methods to generate geometical segment data: contains? and diagonal

geo_bounding_box = GeometricBoundingBox.new_by_arrays([-1, -1], [1, 1])

geo_bounding_box.contains?(GeometricPoint.new(0, 0)) #=> true

Geometrical Polygon:

Use the following method to generate geometical polygon: GeometricBoundingBox.new(vertices)

Use the following methods to generate geometical polygon data: area, bounding_box, contains, and edges

geo_polygon = GeometricPolygon.new([GeometricPoint.new(0,0), GeometricPoint.new(1,1), GeometricPoint.new(0,1)]).area

geo_polygon.area #=> 0.5
geo_polygon.contains?(GeometricPoint.new(0.5, 0.5)) #=> true

Statistics

Statistical Spread:

Use the following methods to generate the statistical spreads of an array:

mean, median, mean, range, min, max, percentile_from_value, value_from_percentile, variance, standard_deviation, relative_standard_deviation, skewness, and kurtosis

stats_spread = StatisticalSpread.new([1,1,2,3,10])

stats_spread.mean #=> 3.4
stats_spread.median #=> 2
stats_spread.mode #=> 1
stats_spread.range #=> 9
stats_spread.min #=> 1
stats_spread.max #=> 10
stats_spread.percentile_from_value(10) #=> 80
stats_spread.value_from_percentile(60) #=> 3
stats_spread.variance #=> 14.299999999999999
stats_spread.standard_deviation #=> 3.7815340802378072
stats_spread.relative_standard_deviation #=> 99.47961485463391
stats_spread.skewness #=> 1.188328915820243
stats_spread.kurtosis #=> 2.405613966453127

Contributing

  1. Fork it ( http://github.com//flash_math/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request