Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross-sum : Function for summing the product of two arrays. #19

Open
Marxsal opened this issue Apr 28, 2018 · 0 comments
Open

Cross-sum : Function for summing the product of two arrays. #19

Marxsal opened this issue Apr 28, 2018 · 0 comments

Comments

@Marxsal
Copy link

Marxsal commented Apr 28, 2018

A frequent scenario is where you have two arrays (or filter runs) of numbers where you want each element from one array to be multiplied by the corresponding element from the other array, and the resulting product to be summed. This is equivalent in a spreadsheet to multiplying two columns by each other and summing the result.

If there's a way to do this with the current set of functions, then I apologize and would be eager to see how to make that work. In the mean time, I came up with the following code below. Note that I don't have it doing the regression like the Sum function does -- I'm not sure that that ability applies in this case.

Thank you
Mark

function Crosssum(a,b) {
if (a instanceof Array && b instanceof Array ) {
var n = 0;
for (var i = 0; i < a.length; ++i) n += (a[i]*b[i]);
return n;
}
return Coerce.ToNum(a,this);
}

exports.crosssum = Crosssum ;

@Marxsal Marxsal changed the title Cross-sum : Functioning for summing the product of two arrays. Cross-sum : Function for summing the product of two arrays. Apr 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant