to.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { factory } from '../../utils/factory.js';
  2. import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js';
  3. var name = 'to';
  4. var dependencies = ['typed', 'matrix', 'concat'];
  5. export var createTo = /* #__PURE__ */factory(name, dependencies, _ref => {
  6. var {
  7. typed,
  8. matrix,
  9. concat
  10. } = _ref;
  11. var matrixAlgorithmSuite = createMatrixAlgorithmSuite({
  12. typed,
  13. matrix,
  14. concat
  15. });
  16. /**
  17. * Change the unit of a value.
  18. *
  19. * For matrices, the function is evaluated element wise.
  20. *
  21. * Syntax:
  22. *
  23. * math.to(x, unit)
  24. *
  25. * Examples:
  26. *
  27. * math.to(math.unit('2 inch'), 'cm') // returns Unit 5.08 cm
  28. * math.to(math.unit('2 inch'), math.unit('cm')) // returns Unit 5.08 cm
  29. * math.to(math.unit(16, 'bytes'), 'bits') // returns Unit 128 bits
  30. *
  31. * See also:
  32. *
  33. * unit
  34. *
  35. * @param {Unit | Array | Matrix} x The unit to be converted.
  36. * @param {Unit | Array | Matrix} unit New unit. Can be a string like "cm"
  37. * or a unit without value.
  38. * @return {Unit | Array | Matrix} value with changed, fixed unit.
  39. */
  40. return typed(name, {
  41. 'Unit, Unit | string': (x, unit) => x.to(unit)
  42. }, matrixAlgorithmSuite({
  43. Ds: true
  44. }));
  45. });