make_diag.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2016 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. //Make a diagonal matrix from a vector or a single value
  14. //If the vector is present the diagonal is the product
  15. #include "eclblas.hpp"
  16. namespace eclblas {
  17. ECLBLAS_CALL void make_diag(bool & __isAllResult, size32_t & __lenResult,
  18. void * & __result, uint32_t m, double v,
  19. bool isAllX, size32_t lenX, const void * x) {
  20. int cells = m * m;
  21. __isAllResult = false;
  22. __lenResult = cells * sizeof(double);
  23. if (lenX > 0 && m*sizeof(double) != lenX) rtlFail(0,"Bad X vector length");
  24. double *diag = (double*) rtlMalloc(__lenResult);
  25. const double *in_x = (const double*)x;
  26. unsigned int r = 0; // row
  27. unsigned int c = 0; //row and column
  28. for (int i=0; i<cells; i++) {
  29. diag[i] = (r==c)? (lenX!=0 ? v*in_x[r] : v) : 0.0;
  30. r++;
  31. if (r==m) {
  32. r = 0;
  33. c++;
  34. }
  35. }
  36. __result = (void*) diag;
  37. }
  38. }