Difference between revisions of "Array.cumcolumn()"

From ZCubes Wiki
Jump to navigation Jump to search
(Created page with " * << Z3 Home * Z3 Language Documentation * Z%5E3_Array_Manipulation_Member_Functions | Listing of Z3 Array Manipulation Member ...")
 
Line 29: Line 29:
 
Each element is accumulated using given function with each element, and with the starting array as first input (if given).
 
Each element is accumulated using given function with each element, and with the starting array as first input (if given).
  
==Array.cumcolumnswith(SomeFunction, SomeStartingValue or Parameter List)==
+
==Array.cumcolumnswith(SomeFunction, SomeColumns ...)==
  
 
[[Array.accumulatewith]] for each column.
 
[[Array.accumulatewith]] for each column.
  
==Array.cumrowswith(SomeFunction, SomeStartingValue or Parameter List)==
+
==Array.cumrowswith(SomeFunction, SomeRows ...)==
==Array.cumcolumnwith(SomeFunction, SomeStartingValue or Parameter List)==
 
==Array.cumrowwith==
 
  
==Array.prototype.cumulate==
+
[[Array.accumulatewith]] for each row
[[Array.accumulatewith]] with function as NUMSUM
 
  
 +
==Array.cumcolumnwith(SomeFunction, SomeStartingValue or Parameter List)==
  
 +
Same as [[Array.accumulatewith]] for each column
  
 +
==Array.cumrowwith==
  
if (!Array.prototype.accumulate)
+
Same as [[Array.cumrowswith]] for each column
{
 
Array.prototype.accumulate= function(SomeTotal)
 
{
 
var SomeTotalArray=new Array();
 
for(var i=0;i<this.length;i++)
 
{
 
// this is correct as we can give a starting array of the possible indices as in ArrayFrequencyRank
 
if(typeof SomeTotal == "undefined")
 
{
 
SomeTotal=0; // good starting for addition.
 
// try
 
// {
 
// SomeTotal=this[i]||0; // this is from a starting array if present.
 
// }
 
// catch(err)
 
// {
 
// SomeTotal=0;
 
// }
 
SomeTotalArray.push(SomeTotal);
 
continue;
 
}
 
 
if(ISARRAY(this[i]))
 
{
 
var SomeThisRowResult = this[i].accumulate(SomeTotal);
 
// take from last summation of the array accumulation? when in multidimensional situation?
 
// Here it pics the last if it is a number, of the FIRST element in the last array to get the rolled up sum of this dimension.
 
SomeTotal+=ISARRAY(SomeThisRowResult.last())?SomeThisRowResult.last()[0]:SomeThisRowResult.last();
 
SomeTotalArray.push(SomeThisRowResult);
 
}
 
else
 
{
 
SomeTotal=NUMSUM(SomeTotal,this[i]);
 
//SomeTotal=SomeTotal+this[i];
 
SomeTotalArray.push(SomeTotal);
 
}
 
}
 
return(SomeTotalArray);
 
}
 
}
 
  
if (!Array.prototype.cumcolumns)
+
==Array.prototype.cumulate==
{
 
Array.prototype.cumcolumns = function()
 
{
 
return(this.transpose().cumrows(ARGS(arguments)).transpose());
 
}
 
}
 
  
if (!Array.prototype.cumrows)
+
[[Array.accumulatewith]] with function as NUMSUM
{
 
Array.prototype.cumrows= function()
 
{
 
var SomeRows;
 
var rowstoget=ARGS(arguments);
 
rowstoget=rowstoget.flatten();
 
if(rowstoget.length==0)
 
{
 
SomeRows=this.rows(); // to get all rows.
 
}
 
else
 
{
 
SomeRows=this.rows(rowstoget);//ARGS(arguments));
 
}
 
 
var SomeNewArray=new Array();
 
for(var i=0;i<SomeRows.length;i++)
 
{
 
SomeNewArray.push(SomeRows[i].accumulate());
 
}
 
return(SomeNewArray);
 
}
 
}
 
Array.prototype.cumcolumn=Array.prototype.cumcolumns;
 
Array.prototype.cumrow=Array.prototype.cumrows;
 
 
 
 
 
if (!Array.prototype.accumulatewith)
 
{
 
Array.prototype.accumulatewith= function(SomeCumulateFunction,SomeCurrentResult)
 
{
 
var SomeTotalArray=new Array();
 
SomeCumulateFunction=GETFUNCTIONSET(SomeCumulateFunction)[0]; // only one allowed here. combine functions if need be to get other results.
 
//SomeCurrentResult=DEFAULT(SomeCurrentResult,0);
 
for(var i=0;i<this.length;i++)
 
{
 
// kept the original code to aid ArrayFrequencyRank
 
// this is correct as we can give a starting array of the possible indices as in ArrayFrequencyRank
 
// still does not make sense since if ArrayFrequencyRank has [0,0] as incoming.
 
// maybe the approach is to apply function to itself to get a starting value?
 
if(typeof SomeCurrentResult== "undefined")
 
{
 
// interesting starting point, assumes that the incoming function may need the entire row as input?
 
SomeCurrentResult=this[i]; // given for each level
 
SomeTotalArray.push(SomeCurrentResult);
 
continue;
 
}
 
if(ISARRAY(this[i]))
 
{
 
var SomeThisRowResult = this[i].accumulatewith(SomeCumulateFunction,SomeCurrentResult);
 
// if the remaining result is an array, take the last element. why?
 
SomeCurrentResult=
 
SomeCumulateFunction
 
.apply(this,[SomeCurrentResult,ISARRAY(SomeThisRowResult.last())?SomeThisRowResult.last()[0]:SomeThisRowResult.last()]);
 
SomeTotalArray.push(SomeThisRowResult);
 
}
 
else
 
{
 
SomeCurrentResult=SomeCumulateFunction.apply(this,[SomeCurrentResult,this[i]]);
 
SomeTotalArray.push(SomeCurrentResult);
 
}
 
}
 
return(SomeTotalArray);
 
}
 
}
 
if (!Array.prototype.cumcolumnswith)
 
{
 
Array.prototype.cumcolumnswith = function(SomeCumulateFunction, SomeColumns)
 
{
 
return(this.transpose().cumrowswith(SomeCumulateFunction,ARGS(arguments).cdr()).transpose());
 
}
 
}
 
if (!Array.prototype.cumrowswith)
 
{
 
Array.prototype.cumrowswith= function(SomeCumulateFunction, SomeRows)
 
{
 
//SomeFunction is the car of ARGS
 
SomeRows=this.rows(ARGS(arguments).cdr()); // overrides
 
 
var SomeNewArray=new Array();
 
for(var i=0;i<SomeRows.length;i++)
 
{
 
SomeNewArray.push(SomeRows[i].accumulatewith(SomeCumulateFunction,SomeRows));
 
}
 
return(SomeNewArray);
 
}
 
}
 
Array.prototype.cumcolumnwith=Array.prototype.cumcolumnswith;
 
Array.prototype.cumrowwith=Array.prototype.cumrowswith;
 
 
 
//Array.prototype.cumulate=Array.prototype.accumulatewith.curry(function(x,y){return(x+y)})
 
Array.prototype.cumulate=Array.prototype.accumulatewith.curry(NUMSUM)
 

Revision as of 13:56, 4 March 2017


Array.accumulate(FromTotal)

Array.accumulate(FromTotal)

Each element is accumulated using SUM with each element.

Array.cumcolumns(Columns|empty)

Each element is accumulated by columns (for the specified columns,or all if none is specified) using SUM with each element.

Array.cumrows(Rows|empty)

Each element is accumulated by rows for the specified rows, or all if none is specified) using SUM with each element.

Array.cumcolumn(Columns|empty)

Same as Array.cumcolumns

Array.cumrow(Row|empty)

Same as Array.cumrows

Array.accumulatewith(SomeFunction, SomeStartingValue or Parameter List)

Each element is accumulated using given function with each element, and with the starting array as first input (if given).

Array.cumcolumnswith(SomeFunction, SomeColumns ...)

Array.accumulatewith for each column.

Array.cumrowswith(SomeFunction, SomeRows ...)

Array.accumulatewith for each row

Array.cumcolumnwith(SomeFunction, SomeStartingValue or Parameter List)

Same as Array.accumulatewith for each column

Array.cumrowwith

Same as Array.cumrowswith for each column

Array.prototype.cumulate

Array.accumulatewith with function as NUMSUM