Guest User

Untitled

a guest
Jan 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. // a function to calculate the convolution of two vectors
  2. // or to multiply the two algebraic expressions.
  3.  
  4. /*
  5. **
  6.  
  7. idea :
  8. ------
  9. vec1 = [2,3,4]
  10. vec2 = [1,2,3]
  11.  
  12. multiply vec2 by vec1[0] = 2 4 6
  13. multiply vec2 by vec1[1] = - 3 6 9
  14. multiply vec2 by vec1[2] = - - 4 8 12
  15. -----------------------------------------------
  16. add the above three = 2 7 14 17 12
  17.  
  18. the - above shows the displacement after each vector multiplication by element of another vector
  19.  
  20. **
  21. */
  22.  
  23. function conv(vec1, vec2){
  24. var disp = 0; // displacement given after each vector multiplication by element of another vector
  25. var convVec = [];
  26. // for first multiplication
  27. for (j = 0; j < vec2.length ; j++){
  28. convVec.push(vec1[0] * vec2[j]);
  29. }
  30. disp = disp + 1;
  31. for (i = 1; i < vec1.length ; i++){
  32. for (j = 0; j < vec2.length ; j++){
  33. if ((disp + j) !== convVec.length){
  34. convVec[disp + j] = convVec[disp + j] + (vec1[i] * vec2[j])
  35. }
  36. else{
  37. convVec.push(vec1[i] * vec2[j]);
  38. }
  39. }
  40. disp = disp + 1;
  41. }
  42. return convVec;
  43. }
  44.  
  45. /*
  46. **
  47.  
  48. Usage:
  49. ------
  50. vecA = [2,3,2,1]
  51. vecB = [4,1,2,3]
  52. ans = conv(vecA, vecB);
  53.  
  54. **
  55. */
Add Comment
Please, Sign In to add comment