# How do I correctly multiply an XMMATRIX by a scalar?

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.

• Thanks for your help, ill 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. – user43129 Mar 8 '14 at 12:50
• but this will not multiply translation part - fourth row! – Yola Aug 16 '14 at 8:15
• 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. – Chuck Walbourn Jul 11 '15 at 16:24

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

• Thanks for the hint but this doesnt work for me! My compiler neither accepts A * f nor f * A. – user43129 Mar 8 '14 at 12:09
• It's weird, it works for me with no problem. I am using Visual studio 12. – János Turánszki Mar 8 '14 at 13:12
• 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. – Chuck Walbourn Aug 19 '14 at 23:24

DirectXMath has the overloads needed for this operation:

struct XMMATRIX
{
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;
XMMATRIX B = A*f;
`