# Multiply vector with scalar value

**URL:** <https://discourse.itk.org/t/multiply-vector-with-scalar-value/2259>\
**Category:** Algorithms\
**Tags:** filter, vector\
**Created:** [September 24, 2019, 8:43am UTC](https://discourse.itk.org/t/multiply-vector-with-scalar-value/2259 "2019-09-24T08:43:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gordian](https://discourse.itk.org/user_avatar/discourse.itk.org/gordian/32/335_2.png) [@Gordian](https://discourse.itk.org/u/Gordian)\
**Post date:** [September 24, 2019, 8:43am UTC](https://discourse.itk.org/t/multiply-vector-with-scalar-value/2259/1 "2019-09-24T08:43:04Z")

</div>

Hello,

i want to multiply either a vector with a single scalar (`s * {v_0,...,v_n} = {s * v_0,...,s * v_n}`) or a elementwise multiplication of two vectors (`{w_0,...,w_n} * {v_0,...,v_n} = {w_0 * v_0,..., w_n * v_n}`).  
This operation shall be apply to an image of vectors.

The obvious choice would be the [MultiplyImageFilter](https://itk.org/Doxygen/html/classitk_1_1MultiplyImageFilter.html) but once template it over a VectorImage the constant has to be a vector.  
This example does not work because `factor` is not a vector:

```
auto multiply = itk::MultiplyImageFilter<VectorImageType>::New();
multiply->SetInput(vectorImage);
multiply->SetConstant(factor);

```

This does not work either:

```
auto multiply = itk::MultiplyImageFilter<VectorImageType>::New();
itk::Image<itk::CovariantVector<double, 3>, 3>::PixelType factor;
factor.Fill(1);
factor[2] = 100;
multiply->SetInput(vectorImage);
multiply->SetConstant(factor);

```

All the output values are multiplied as a vector multiplication.

Is there a filter I have not considered because its not obvious?  
Is this type of operation done by an accessor or castimage filter?

Thanks,  
Gordian

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [September 24, 2019, 1:34pm UTC](https://discourse.itk.org/t/multiply-vector-with-scalar-value/2259/2 "2019-09-24T13:34:24Z")

</div>

I think that [BinaryGeneratorImageFilter](https://itk.org/Doxygen/html/classitk_1_1BinaryGeneratorImageFilter.html) can do what you want. You will need to define a per-pixel operation via a lambda function or a functor. You can read more about it [here](https://discourse.itk.org/t/new-functor-filter-with-lambda-support/899).

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [September 24, 2019, 2:32pm UTC](https://discourse.itk.org/t/multiply-vector-with-scalar-value/2259/3 "2019-09-24T14:32:23Z")

</div>

> [@Gordian](#):
>
> This example does not work because `factor` is not a vector:
> 
> ```auto
> auto multiply = itk::MultiplyImageFilter<VectorImageType>::New();
> multiply->SetInput(vectorImage);
> multiply->SetConstant(factor);
> 
> ```

To multiply by a scalar constant the second image type needs to be a scalar. The full class template signature is:

```auto
itk::MultiplyImageFilter< TInputImage1, TInputImage2, TOutputImage > 

```

Try something like:

```auto
using ScalarImageType = itk::Image<double,ImageDimension>;
auto multiply = itk::MultiplyImageFilter<VectorImageType, ScalarImageType, VectorImageType>::New()
multiply->SetInput(vectorImage);
multiply->SetConstant2(factor);

```
