Physics quantities (See related posts)

// Physics quantities
// needs:<script src=gnmath.txt>script>
// needs: <script src=gnrandom.txt>script>
// ******************************************************************************
// ******************************************************************************
// ******************************************************************************
// ******************************************************************************
// Unit of Misur CLASS (UM) ***************************************
// an element is a string like m^2 k^3 s^(-1)
// its rappresentatios is:  [["m",2],["k",3],["s",-1]]
// (using gnmath constants definition we can also use: [[_m,2],[_k,3],[_s,-1]]
function UM(zxc){ 
        this.vv=zxc;
}
new UM([['dummy',0]]);


UM.prototype.toString=function(){
        this.collect();
        s=""
        for (var i=0;i<this.vv.length;i++){
                s+=' '
                s+=this.vv[i][0];
                s+=(this.vv[i][1]!=1)? ''+this.vv[i][1]+' ': ' '
        }
        return s;
}


        // +++++++++++ make the nth power of um
UM.pow=function(umis,n){// umis : is a UM!!
//     wwl('inUM.pow azsx='+azsx)
        for (var i=0;i<umis.vv.length;i++){
                umis.vv[i][1]*=n; // the name rest the same,the exponent is multiplied
        }
//     wwl('inUM.pow prima di uscire azsx='+azsx)

        return umis;
}




        // +++++++++++ collect same units
UM.prototype.collect=function(){
//     alert('collecting:'+this.vv)
        for (var i=0;i<this.vv.length;i++){
                for (var j=i+1;j<this.vv.length;j++){
                        if(this.vv[i][0]==this.vv[j][0]){
                                this.vv[i][1]+=this.vv[j][1]; // sum exponents
                                this.vv[j][0]="";this.vv[j][1]=""} // nill last term equal
                        }
                }
//     alert('collected:'+this.vv)

        for (var i=0;i<this.vv.length;i++){// nill null exponent term
                        if(this.vv[i][1]==0){this.vv[i][0]="";this.vv[i][1]=""}
        }
//     ww('collected e nilled:'+this.vv)



        this.vv.sort(); // sort um alphabetically
//     ww('collected e sorted:'+this.vv)

        return this.vv;
}




        // +++++++++++++ multiply(x,y)  returns a string with um multiplied 
UM.multiply=function(x,y){
        var k=0;
        newVett=new Array() // a new array will collect all terms from two strings



        for(var i=0;i// loading first element terms
                var a1=new Array();
                        a1[0]=x.vv[i][0]  // um name
                        a1[1]=x.vv[i][1]  // um exponent
                newVett[i]=a1;
        }
        for(var j=0;j<y.vv.length;j++){// // loading second element terms
                var a2=new Array();
                        a2[0]=y.vv[j][0]     // um name
                        a2[1]=y.vv[j][1]     // um exponent
                newVett[i+j]=a2;
        }


//    ww('creo prodotto')
//     ww(newVett)
        var prodotto = new UM(newVett);// build & return a new um with data collected
//     ww('pp:',prodotto)
        prodotto.collect();
//     ww('pp2:',prodotto)
        return prodotto;
}





        // ++++++++++++++++ x.divide(y) returns a string with um divided
UM.divide=function(x,y){
//     alert('x='+x)
//     alert('y='+y)
        var k=0;
        newVett=new Array() //vettore che conterr� vettori da due
        for(var i=0;i// carico tutti quelli di x
                var a1=new Array();
                a1[0]=x.vv[i][0]
                a1[1]=x.vv[i][1]
                newVett[i]=a1;
        }
//     alert(newVett)




        for(var j=0;j<y.vv.length;j++){// carico tutti quelli di y
                var a2=new Array();
                a2[0]=y.vv[j][0]
                a2[1]=-y.vv[j][1] // being at den power have to be inverted
                newVett[i+j]=a2;
        }
//    alert(newVett)



        var rapporto=new UM(newVett);
//     ww('rapp1='+rapporto)
        rapporto.collect();
//     ww('rapp2='+rapporto)

        return rapporto;
}





// ******************************************************************************
// ******************************************************************************
// ******************************************************************************


// ******************************************************************************
// ******************************************************************************
// ******************************************************************************
// Physical Quantity CLASS  (PQ)
// an element is a vector including a number and a um string  like (8*10^4) m^2 k^3 s^(-1)
// its rappresentatios is:  [8,4,um] ie [ 8, 4 ,  [[_m,2],[_k,3],[_s,-1]] ]



function PQ(_x,_exp,_mis){ //argomenti: x= numero; exp= potenza dieci; _mis um
        this.x=_x;
        this.exp=_exp;
        // POLIMORPHISM: if _mis is a vector:
        if(_mis.length >0 ){
                this.mis=new UM(_mis);
        }
        else{ // if _mis is already a um:
                this.mis=_mis
        }
}

new PQ(0,0,[['dummy',0]])
PQ.precision=3;
PQ.prototype.toString=function (){
        this.mis.collect();
        return (
                trunc(this.x,PQ.precision)+
                ( (this.exp==0)? '':''+dot+''+'10 '+this.exp+' '  ) +
                this.mis.toString()+' '
        )
}
PQ.prototype.clone=function(){
        var s="";
        s+=this.x;
        s+=','+this.exp
        var ss="[ "
        var comma=','
        for (var i=0;i<this.mis.vv.length;i++){
                if(i==this.mis.vv.length-1){comma=''}
                ss+= "['" + this.mis.vv[i][0] + "'," + this.mis.vv[i][1] + "]"+comma
        }

        ss+=' ]'
        s+=','+ss
        return s;
}


PQ.multiply=function(x,y){
        return new PQ( x.x*y.x, x.exp+y.exp ,UM.multiply(x.mis,y.mis) );
}



PQ.pow=function(x,n){
//     wwl('inPQ.pow azsx='+azsx)
        return (new PQ( Math.pow(x.x,n), x.exp * n ,UM.pow(x.mis,n))  );
}

PQ.divide=function(x,y){
        return new PQ( x.x/y.x, x.exp-y.exp ,UM.divide(x.mis,y.mis));
}



PQ.sum=function(x,y){
        var z=x.x*Math.pow(10,x.exp)+y.x*Math.pow(10,y.exp)
        return new PQ( z, 0 ,x.mis);
}

PQ.diff=function(x,y){
        var z=x.x*Math.pow(10,x.exp)-y.x*Math.pow(10,y.exp)
        return new PQ( z, 0 ,x.mis);
}



PQ.scalarMultiply=function(x,n){
        var z=(x.x*Math.pow(10,x.exp) ) *n
        return new PQ( z, 0 ,x.mis);
}

PQ.prototype.normalize=function(){
        // Adjust mantissa to x.yyy
        var og=Math.round(log10(this.x));
        this.x/=Math.pow(10,og);
        this.exp=(this.exp*1+og*1)
        return this;
}
//***********************



You need to create an account or log in to post comments to this site.


Related Posts