numpy - how to calculate the dot product of two arrays of vectors in python? - Stack Overflow
Dot product of two arrays in Javascript - Stack Overflow
Can someone explain what is happening here? I'm using numpy and I don't really understand the dot product
Dot product of two 3x3x3 arrays [University maths]
Videos
I would like to compute the dot product of two arrays of numbers, x and y. One way is as follows:
int dot_product (
int *x, /* first array */
int *y, /* second array */
int n /* size of arrays */
) {
int dot_product = 0 ;
for(int i = 0 ; i < n ; i++) {
dot_product += x[i] * y[i] ;
}
return dot_product ;
}
This covers the case where x and y are arrays of ints, but I would like to also support the cases where one or more of x, y is an array of floats. The return type is not very important; it can be a double for all cases.
A solution is to cover all possibilities with 7 arguments:
double dot_product (
int *x_int_1,
int *y_int_1,
int *x_int_2,
double *y_double_2,
double *x_double_3,
double *y_double_3,
int n
) {
int i ;
double dot_product = 0 ;
if (x_int_1 != NULL y_int_1 != NULL) {
for(i = 0 ; i < n ; i++) {
dot_product += x_int_1[i] * y_int_1[i] ;
}
}
else if (x_int_2 != NULL y_double_2 != NULL) {
for(i = 0 ; i < n ; i++) {
dot_product += x_int_2[i] * y_double_2[i] ;
}
}
else {
for(i = 0 ; i < n ; i++) {
dot_product += x_double_3[i] * y_double_3[i] ;
}
}
return dot_product ;
}However, this is not a very good solution. What is a good way to do it?
dot = (a, b) => a.map((x, i) => a[i] * b[i]).reduce((m, n) => m + n);
console.log(dot([1,2,3], [1,0,1]));
Here we use Array.prototype.map to create a new array with multiplied results of each index and Array.prototype.reduce to sum the values of resulting array.
Benchmark (the comments start with the median time of a thousand runs in ms):
let opt=[
// (0.65) like kyun's answer but without extra variable
'v.reduce((l,r,i)=>l+r*w[i],0)',
// (0.66) like kyun's answer
'v.reduce((l,r,i)=>{l+=(r*w[i]);return l},0)',
// (0.71) variable for length declared outside block
'let s2=0,l2=v.length;for(let i2=0;i2<l2;i2++)s2+=v[i2]*w[i2]',
// (0.72) block-scoped variable for length
'let s=0;for(let i=0,l=v.length;i<l;i++)s+=v[i]*w[i]',
// (1.20) like the accepted answer
'v.map((_,i)=>v[i]*w[i]).reduce((l,r)=>l+r)',
// (1.93) hardcoded number for length
'let s1=0;for(let i1=0;i1<1e4;i1++)s1+=v[i1]*w[i1]',
// (2.05) check length during each iteration
'let s3=0;for(let i3=0;i3<v.length;i3++)s3+=v[i3]*w[i3]',
// (6.25) no `let` for sum variable
's4=0;for(let i4=0,l4=v.length;i4<l4;i4++)s4+=v[i4]*w[i4]',
// (12.17) no `let`
's5=0;l5=v.length;for(i5=0;i5<l5;i5++)s5+=v[i5]*w[i5]',
// (16.36) `var` instead of `let`
'var s6=0,l6=v.length;for(var i6=0;i6<l6;i6++)s6+=v[i6]*w[i6]'
]
opt.sort(()=>Math.random()-.5)
let v=Array.from({length:1e4},()=>Math.random())
let w=Array.from({length:1e4},()=>Math.random())
for(let opti of opt){
let t1=process.hrtime.bigint()
eval(opti)
let t2=process.hrtime.bigint()
console.log(t2-t1+'\t'+opti)
}
In order to reduce the impact of optimizations for running the same code multiple times, I ran the benchmark like for i in {0..999};do node script.js;done instead of running each option 1000 times inside the script.
Here's the code I'm trying to understand
In the first example there's 2 arrays both on the 0th axis (I believe) and the output is a single number
In the second one, there's 2 arrays as well, but one is one the 0th axis and one is on the 1st (I'm pretty sure, correct me if I'm wrong) and in this example my output is a 2D, 3x3 array
Why is this happening? Can someone explain? I tried googling it but I found examples where both occur so I'm not sure
Hello
I've been doing some work in Python for my research project and I've come over a solution to a problem which has my supervisor skeptical and myself confused.
Say I have two arrays: X = Y = [[[ 1. 1. 1.], [ 1. 1. 1.], [ 1. 1. 1.]],
[[ 1. 1. 1.], [ 1. 1. 1.], [ 1. 1. 1.]],
[[ 1. 1. 1.], [ 1. 1. 1.], [ 1. 1. 1.]]]
The shape of the array is 3x3x3.
Now, if I take the dot product ( R = np.dot(X,Y) ) I get a 3x3x3x3 array:
R = [[[[ 3. 3. 3.], [ 3. 3. 3.], [ 3. 3. 3.]],
[[ 3. 3. 3.], [ 3. 3. 3.], [ 3. 3. 3.]],
[[ 3. 3. 3.], [ 3. 3. 3.], [ 3. 3. 3.]]],
[[[ 3. 3. 3.], [ 3. 3. 3.], [ 3. 3. 3.]],
[[ 3. 3. 3.], [ 3. 3. 3.], [ 3. 3. 3.]],
[[ 3. 3. 3.], [ 3. 3. 3.], [ 3. 3. 3.]]],
[[[ 3. 3. 3.], [ 3. 3. 3.], [ 3. 3. 3.]],
[[ 3. 3. 3.], [ 3. 3. 3.], [ 3. 3. 3.]],
[[ 3. 3. 3.], [ 3. 3. 3.], [ 3. 3. 3.]]]]
How did I increase the dimensions of the array by one? Where can I read up on this sort of thing? Thanks!
Edit: From the scipy manual on numpy -
dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m]) For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors (without complex conjugation). For N dimensions it is a sum product over the last axis of a and the second-to-last of b: