# How to keep result one-dimensional even after multiplication?

**URL:** <https://discourse.julialang.org/t/how-to-keep-result-one-dimensional-even-after-multiplication/46439>\
**Category:** New to Julia\
**Tags:** question, arrays, vector\
**Created:** [September 11, 2020, 8:25am UTC](https://discourse.julialang.org/t/how-to-keep-result-one-dimensional-even-after-multiplication/46439 "2020-09-11T08:25:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ishihama](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@ishihama](https://discourse.julialang.org/u/ishihama)\
**Post date:** [September 11, 2020, 8:25am UTC](https://discourse.julialang.org/t/how-to-keep-result-one-dimensional-even-after-multiplication/46439/1 "2020-09-11T08:25:09Z")

</div>

I am trying several functions who include vectors as variable. The variable (x) is

```julia
x= [2.0:0.5:5.0;]

```

I like to define the following kinds of functions:

```julia
f(x) = (x .+ 0.07im)/2.3;
g(x) = (1/f(x)).^2
h(x) = g(x) .* (2 .- (1 .+ f(x)).^0.5 - (1 .- f(x)).^0.5)

```

In this case, the function `h` becomes 7x7 squared matrix, which I do not wish.  
One dimensional vector is preferable for me. If I append [1] at the last part of h(x)  
statement, I can work it around somehow.  
To be honest, I prefer keeping my codes as simple as possible.  
Removing “a dot before asterisk” resulted in a matrix having only one element,  
which turns also out to be useless. Any idea?

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [September 11, 2020, 8:37am UTC](https://discourse.julialang.org/t/how-to-keep-result-one-dimensional-even-after-multiplication/46439/2 "2020-09-11T08:37:58Z")

</div>

While `ones(3) / 3` scales the vector, the same as `./` broadcasting, `y = 3 / ones(3)` solves a linear system such that `y * ones(3) == 3` where `*` means matrix multiplication. You might be looking for `@.`, to apply all functions element-wise.

---

<div class="post-metadata">

**Author:** ![ishihama](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@ishihama](https://discourse.julialang.org/u/ishihama)\
**Post date:** [September 11, 2020, 9:14am UTC](https://discourse.julialang.org/t/how-to-keep-result-one-dimensional-even-after-multiplication/46439/3 "2020-09-11T09:14:31Z")

</div>

Thanks a lot. I will learn again about mapping and broadcasting. I think removing dots from function definitions and apply dot in the final calculation like `h.(x)`. I will try it.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 11, 2020, 9:32am UTC](https://discourse.julialang.org/t/how-to-keep-result-one-dimensional-even-after-multiplication/46439/4 "2020-09-11T09:32:41Z")

</div>

Yes, that is exactly the right approach. Define your functions for scalar input, and use dots when you call them. That is both cleanest and best for performance.
