matrix-multiply.hpp 792 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #pragma once
  2. //matrix multiplication primitives
  3. //used in: ruby/opengl/quark
  4. namespace nall {
  5. template<typename T> inline auto MatrixMultiply(
  6. T* output,
  7. const T* xdata, uint xrows, uint xcols,
  8. const T* ydata, uint yrows, uint ycols
  9. ) -> void {
  10. if(xcols != yrows) return;
  11. for(uint y : range(xrows)) {
  12. for(uint x : range(ycols)) {
  13. T sum = 0;
  14. for(uint z : range(xcols)) {
  15. sum += xdata[y * xcols + z] * ydata[z * ycols + x];
  16. }
  17. *output++ = sum;
  18. }
  19. }
  20. }
  21. template<typename T> inline auto MatrixMultiply(
  22. const T* xdata, uint xrows, uint xcols,
  23. const T* ydata, uint yrows, uint ycols
  24. ) -> vector<T> {
  25. vector<T> output;
  26. output.resize(xrows * ycols);
  27. MatrixMultiply(output.data(), xdata, xrows, xcols, ydata, yrows, ycols);
  28. return output;
  29. }
  30. }