Showing posts with label Mathematics. Show all posts
Showing posts with label Mathematics. Show all posts

20220428

Strategic search

If we divide a circle into 360 equal angles, we end up with one degree incremental vectors in which we can look around ourselves on a planar surface. When we misplace something, we can head off in 360 different directions. If we then allow for up and down angles of equivalent size, then for the sphere surrounding us at every moment, there are 41,253 directions we can look. Given all these options, we must choose wisely.

Technically, the number of directions is computed from A=4πr² where r=360/2π. [Square degree]

20190630

easeSmoother

The CSS cubic-bezier timing function creates easing curves using 4 control points.

The first and last control points are fixed at (0,0) and (1,1), while the second and third control points are passed as arguments to create a variety of easing curves.

Sometimes an "in-out" easing is needed which starts slow and ends slow. We experimented to find a curve that best approximates the smootherStep function (see here) which produces a natural acceleration similar to when we change lanes while driving on the highway or when we reach for something (see also this).

Specifying the timing function as "cubic-bezier(.49,0,.51,1)" creates the lowest error compared to the minimum-jerk reference (see code at the bottom of this article). Visualize the timing here using Lea Verou’s cubic-bezier.com (compare to ease-in-out). JSFiddle example: https://jsfiddle.net/intrinsica/9ryqet30

If a preset it created, it could be aliased as easeSmoother or easeInOutSmoother.

The green curve below is the reference min-jerk function. The purple curve is the easeSmoother cubic-bezier.



JavaScript reference (see also):

function MinJerk(t) {
  var ts = t * t, tc = ts * t;
  return 6*tc*ts - 15*ts*ts + 10*tc;
}

Alternatively:

let MinJerk = t => (6*t*t-15*t+10)*t*t*t;

Below is the L2 error curve, comparing the cubic-bezier to the reference min-jerk function.

20161107

Faster hypot for JavaScript

When the distance between two points is required, one method of computation in modern browsers is a function called Hypot. Implementation of this function is often not optimized and ultimately calls on Math.sqrt at the last step. Performance tests indicate a calculation bottleneck with JavaScript's standard Math library implementation. Here then is an improvement of hypot at the cost of a fractional error (0.05%):

  function hypot(x, y) { // expects: abs(x) and abs(y)
    var a = Math.max(x,y),
        b = Math.min(x,y)/a,
        c = 1 + b*b;
    return a * ((-0.0705613450914645*c + 0.624084374908536)*c + 0.447383574908535);
  }

Note, this function expects absolute values (which can be computed on the call). The formula in the return is a polynomial approximation of the square root of values in the range from 1 to 2. Caveat: your mileage may vary!

20161102

Neural Network in 12 lines of JavaScript

Inspired by Andrew Trask's article about coding a neural network in 11 lines of Python, I wondered if this could be accomplished as succinctly in JavaScript. Part of the brevity of the Python solution is the use of a common numeric library called Numpy which helps manipulate matrix mathematics. For something comparable, I rolled my own JavaScript matrix library to assist with some of the notation and computation. The implementation converges quickly on the XOR solution as represented by the box moving toward the outlined target. Explore the solution on the newly revamped OpenProcessing website and embedded here:
20161112 Update: Just noticed this similar implementation.

20160306

Sphiral Minima

1000 dots perform a coordinated dance. How many combinations could there be? It's surprising! Tap/click to advance along the time continuum (the x value is added to the timeline, so clicking near the left side advances more slowly than clicking near the right side)...amazing harmonic patterns await. WARNING: there is a naturally occurring stroboscopic effect at times. This sketch is a reduction of http://hakim.se/experiments/html5/sphere.

20150213

A memorable approximation of pi

One of the best-known approximations of π is the fraction 22/7. Another is 355/113 which is easy to memorize by remembering the first three odd numbers, 1,3, and 5. Next, double the digits: 11 33 55, then group as 113 355, and finally slide the 113 under the 355.

Upon examining how close the result of 355/113 comes to the real value of π, it made sense to find another memorable fraction that approximates that difference which could then be summed. Repeating the process once more yields a formula accurate to the first 17 digits of π. (Perhaps we'll name it the "Dunn Approximation of Pi" after its discoverer.)






which, when computed carefully, yields: 3.1415926535897932 762597293465706

(which is greater than π by a mere 37.8 quintillionth, or more precisely, 3.7797085963291064091099708947939e-17)
(which is enough accuracy to compute the diameter of the earth with an error of only 1 nanometer. For reference, the thickness of a sheet of paper is about 100,000 nanometers!)

Most importantly, for memorization: the second term can be thought of as 33 78 99 (then slide the first three digits under the last three, just like we did with the first term). Next, think of the final term as 10 777 (this time, invert the slide, moving the last two 77s under the 107). What remains to be remembered are the signs and the adjusting multipliers of "7" (notice there are five of them in the equation) and "12" (which is, of course, five higher than 7).

In JavaScript, the formula looks like: var pi=355/113-1e-7*899/337+1e-12*107/77;
(yields 3.141592653589793 56009 )

In Excel or LibreOffice Calc, paste into a cell: =355/113-1e-7*899/337+1e-12*107/77
(3.14159265358979 00000)

In Java:
double t1=(double)355f/(double)113f;
double t2=(double)1e-7*(double)899f/(double)337f;
double t3=(double)1e-12*(double)107f/(double)77f;
double pi=t1-t2+t3;
(3.14159265358979 05)

In numcalc.com with 192-bit precision:
355/113-10^-7*(899/337)+10^-12*(107/77)
(yields 3.1415926535897932 76259729346570553471632750013044648164525)

Rational approximations were explored and computed using a modified version of this tool written in Perl.

See also this visualization of π approximations.

For reference, here are additional digits of π: 3.1415926535897932 3846264338327950288419716939937510 58209749445923078164062862089986280 348253421170679821480865132823066470 9384460955058223172535940 812848111745028410

20131025

Formulas for Squircles

The Desmos online graphing calculator is useful for visualizing mathematical equations such as the squircle.

The formula for the red circular arc is for comparison. The blue line is the squircle and the orange and green lines are higher order shapes. Odd values produce tangents. Closer inspection of the higher order shapes reveals a gap above of the x-axis. Why is this?
















Additionally, the calculator supports sliding variables so the numbers 2,4,6,8 above can be replaced by the variable "a" in a single equation and assigned a stepping value of 2. (Also try 0.2)


KVR News:

The Gadget Blog | Latest Gadgets, Technology