Using DirectXMath and its XMMATRIX structure in C++ and Direct X 11, how does one multiply that matrix structure by a single float scalar?

I want to implement the operation B = A * f; where A and B are XMMATRIX and f is a float.

I found all sorts of functions to multiply a matrix by another matrix or a vector. I found all sorts of functions to construct matrices.

I could find no scalar multiplication! Why is there no such function? Is there no use case? Did I miss something? How do I implement scalar multiplication?


It appears from documentation that the multiplication operator you use only does matrix-matrix multiplication.

The most straight-forward way to scale by a scalar is to matrix multiply your matrix with XMMatrixScaling(f,f,f), or to scale each of the row vectors making up the XMMATRIX by a scalar.

If I had to hazard a guess, I would reckon that the library is designed around exposing fast vectorized functions for the common expensive operations that are error-prone or hard to implement as an end user. Judging by the current other answer, it seems like there's more convenience functionality in more recent versions of the Windows Kits which includes the operators you seek.

  • \$\begingroup\$ Thanks for your help, i`ll try it out. But, off topic: What kind of an API is that supposed to be??? Not intuitive at all. Something must be wrong here, I feel like. \$\endgroup\$ – user43129 Mar 8 '14 at 12:50
  • \$\begingroup\$ but this will not multiply translation part - fourth row! \$\endgroup\$ – Yola Aug 16 '14 at 8:15
  • 1
    \$\begingroup\$ You may find using SimpleMath in the DirectX Tool Kit more intuitive and an easier approach to learning DirectXMath. For more on DirectXMath generally, see this blog post. \$\endgroup\$ – Chuck Walbourn Jul 11 '15 at 16:24

There is a multiplication operator. You can multiply XMMATRIX a by scalar b with a*b.

  • \$\begingroup\$ Thanks for the hint but this doesnt work for me! My compiler neither accepts A * f nor f * A. \$\endgroup\$ – user43129 Mar 8 '14 at 12:09
  • \$\begingroup\$ It's weird, it works for me with no problem. I am using Visual studio 12. \$\endgroup\$ – János Turánszki Mar 8 '14 at 13:12
  • 1
    \$\begingroup\$ DirectXMath functions including the operators are in the DirectX namespace. If you do not have using namespace DirectX; in your current scope, the operator overloads will not be found. \$\endgroup\$ – Chuck Walbourn Aug 19 '14 at 23:24

DirectXMath has the overloads needed for this operation:

    XMMATRIX&   operator*= (float S);
    XMMATRIX    operator* (float S) const;
    friend XMMATRIX     XM_CALLCONV     operator* (float S, FXMMATRIX M);

Likely the problem you are having is that you do not have the proper namespace scope active to find the operator* overload. Try

using namespace DirectX;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.