Friday, January 2, 2015

Math Object Challenge: Update #2.

I have made substantial progress; using Taylor series, I approximated the sin, cos, sec, and csc functions. I also added summation and product and a few others. I now, however, have to lean on the use of ln and exp functions from the math object. I added a new function 'truncIf' that truncates a number if and only if the other parameter is an integer (that is, equal it itself when truncated). It is used to give more accurate results when using the pow command.

Code:
var atan = Math.atan;
var tan = Math.tan;
var pi = Math.PI;
var ln = Math.log;
var exp = Math.exp;

function ceil(x){
return (sgn(x))*(abs(x)+.5+div(atan(-tan(pi*(abs(x)+.5))),pi))+(x<0?1:0);
}

function floor(x){
return -ceil(-x);
}

function abs(x){
return sgn(x)*x;
}

function sgn(x){
return (x<0)?-1:(x>0)?1:0;
}

function div(x,y){
return x/y;
}

function sin(y){
var sign = sgn(y);
var x = abs(y);
while(x>2*pi){
x-=2*pi;
}
return (x - (pow(x,3)/fac(3)) + (pow(x,5)/fac(5)) - (pow(x,7)/fac(7)) + (pow(x,9)/fac(9)) - (pow(x,11)/fac(11)) + (pow(x,13)/fac(13)) - (pow(x,15)/fac(15)) + (pow(x,17)/fac(17)))*sign;
}

function cos(y){
var sign = sgn(y);
var x = abs(y);
while(x>2*pi){
x-=2*pi;
}
return (1 - (pow(x,2)/fac(2)) + (pow(x,4)/fac(4)) - (pow(x,6)/fac(6)) + (pow(x,8)/fac(8)) - (pow(x,10)/fac(10)) + (pow(x,12)/fac(12)) - (pow(x,14)/fac(14)) + (pow(x,16)/fac(16)))*sign;
}

function sec(x){
return 1/cos(x);
}

function csc(x){
return 1/sin(x);
}

function fac(x){
return product(1,x,"#");
}

function product(start,end,transform,inst){
var inst = inst || "#";
var out = 1;
transform = transform.split("");
for(i=0;i<transform.length;i++){
transform[i] = transform[i].replace(inst,"i");
}
transform = transform.join("");
for(i=start;i<=end;i++){
out*=eval(transform);
}
return out;
}

function sum(start,end,transform,inst){
var inst = inst || "#";
var out = 1;
transform = transform.split("");
for(i=0;i<transform.length;i++){
transform[i] = transform[i].replace(inst,"i");
}
transform = transform.join("");
for(i=start;i<=end;i++){
out+=eval(transform);
}
return out;
}

function pow(x,y){
return truncIf(x+y,exp(y*ln(x)));
}

function trunc(x){
return +(x+"").split(".")[0];
}

function truncIf(x,y){
return (trunc(x)==x)?trunc(y):y;
}

Thursday, January 1, 2015

Challenge initiated: Let's re-create the Math object.

First, a public apology: I'm moving away from the syntax highlighter. Sorry, :(, but I don't have the time and energy to use it. So, just a grey-and-white PRE. So, right now, I'm attempting at re-creating all of the functions in the Math object. Typically, this wouldn't be to hard, however; I am trying to define as much of the functions mathematically, that is, with equations, leaning only on the functions I define myself. Currently, I have very few functions covered; I am also currently leaning slightly on the Math object for two functions and a variable: Math.atan, Math.tan, and Math.PI; I can easily define the last one, and possibly the second with a Taylor sequence, but I haven't gotten around to doing this.
var atan  = Math.atan;
var tan   = Math.tan;
var pi    = Math.PI;

function ceil(x){
 return (sgn(x))*(abs(x)+.5+div(atan(-tan(pi*(abs(x)+.5))),pi))+(x<0?1:0);
}
function floor(x){
 return -ceil(-x);
}
function abs(x){
 return sgn(x)*x;
}
function sgn(x){
 return (x<0)?-1:(x>0)?1:0;
}
That's all for now, I hope to have more for you tomorrow.