# Mutation of a scalar variable

**URL:** https://discourse.julialang.org/t/mutation-of-a-scalar-variable/63798
**Category:** New to Julia
**Tags:** numbers, array, vector
**Created:** [June 30, 2021, 5:18am UTC](https://discourse.julialang.org/t/mutation-of-a-scalar-variable/63798 "2021-06-30T05:18:25Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 30, 2021, 5:18am UTC](https://discourse.julialang.org/t/mutation-of-a-scalar-variable/63798/1 "2021-06-30T05:18:25Z")

</div>

Hi,

I’d like to mutate a scalar variable.  
For example, if `x` is a vector, then

```julia
x .= x_new

```

works fine for mutation.  
However, when `x` is a `Number`, e.g., `x = 1.0`,

```julia
x .= x_new

```

does not work properly.  
Also, I cannot find a way to mutate a scalar variable `x`.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [June 30, 2021, 5:26am UTC](https://discourse.julialang.org/t/mutation-of-a-scalar-variable/63798/2 "2021-06-30T05:26:23Z")

</div>

`1.0` is a `Float64` which is immutable, it cannot be mutated. You should instead just rebind it

```julia
x = x_new

```

Why did you want to mutate a number?

---

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 30, 2021, 5:29am UTC](https://discourse.julialang.org/t/mutation-of-a-scalar-variable/63798/3 "2021-06-30T05:29:35Z")

</div>

For some reason, my code is well-defined with mutation of arrays.  
However, when I’m trying to deal with the case of an array whose length is 1,  
it is quite uncomfortable (for example, I have to use `x .* x` or `x[1] * x[1]` instead of `x * x`).  
So I wonder if there is a way of mutating scalar variable.

---

<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: [June 30, 2021, 5:45am UTC](https://discourse.julialang.org/t/mutation-of-a-scalar-variable/63798/4 "2021-06-30T05:45:33Z")

</div>

You cannot mutate scalar values.

But a 1x1 array is not a scalar, and you can mutate it just fine. Also, you can do `x*x`:

```julia
jl> x = rand(1,1)
1×1 Matrix{Float64}:
 0.040743095726171186

jl> x * x
1×1 Matrix{Float64}:
 0.0016599998493519487

```

It doesn’t work for vectors, though, since multiplication isn’t defined for vectors:

```julia
jl> x = rand(1)
1-element Vector{Float64}:
 0.5736316758843656

jl> x * x
ERROR: MethodError: no method matching *(::Vector{Float64}, ::Vector{Float64})

```

In what way is `x .* x` a problem?

---

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 30, 2021, 5:47am UTC](https://discourse.julialang.org/t/mutation-of-a-scalar-variable/63798/5 "2021-06-30T05:47:27Z")

</div>

Thank you.  
Actually, there is no problem with `x .* x`. I just thought that it’s not intuitive to me (as it is not of my coding pattern).  
BTW, make it `Matrix` seems nice 🙂

---

<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: [June 30, 2021, 5:51am UTC](https://discourse.julialang.org/t/mutation-of-a-scalar-variable/63798/6 "2021-06-30T05:51:45Z")

</div>

My advice is to get used to the dot. In my opinion it’s one of the coolest and most powerful features of Julia.

---

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 30, 2021, 6:00am UTC](https://discourse.julialang.org/t/mutation-of-a-scalar-variable/63798/7 "2021-06-30T06:00:25Z")

</div>

Indeed 🙂  
Thank your for your advice.

Actually, the main reason I wanted to mutate a scalar variable is to make it compatible with [in-place method of DifferentialEquations.jl](https://diffeq.sciml.ai/stable/basics/problem/#In-place-vs-Out-of-Place-Function-Definition-Forms), while not changing variable’s type (i.e., remaining it as scalar).  
In my experience, changing type of variables make many troubles. For example, I may have to change previous codes. Or, I may have to restart Julia REPL.

One remedy that I just thought is to utilise [ComponentArrays.jl](https://github.com/jonniedie/ComponentArrays.jl) and assign a scalar into a ComponentArray (I’m not sure it works for in-place method of DiffEq.jl when a component of ComponentArray is scalar).  
I’ll give a try and share my experience.

---

<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: [June 30, 2021, 6:12am UTC](https://discourse.julialang.org/t/mutation-of-a-scalar-variable/63798/8 "2021-06-30T06:12:39Z")

</div>

I think this is probably not the best approach. _Non-mutating_ operations on scalars are faster than mutating operations on arrays.

Scalars are not compatible with in-place versions of algorithms, and it’s better to not use them in that. Can’t you just use scalar versions?

---

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 30, 2021, 6:14am UTC](https://discourse.julialang.org/t/mutation-of-a-scalar-variable/63798/9 "2021-06-30T06:14:49Z")

</div>

I hope so but I’ve not found a good way to use out-of-place method for [SimulationLogger.jl](https://github.com/JinraeKim/SimulationLogger.jl), which is a convenient logging tool for DiffEq.  
Unfortunately, I have to detour this issue till making it compatible with out-of-place (oop) method ☹

EDIT: hmm… maybe it’s the time to consider to make it compatible with oop method.

---

<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: [June 30, 2021, 6:37am UTC](https://discourse.julialang.org/t/mutation-of-a-scalar-variable/63798/10 "2021-06-30T06:37:30Z")

</div>

I see. I’m not familiar with those tools. If you are forced to use mutation anyway, you can look up `Ref`, and see if that helps.

---

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 30, 2021, 6:38am UTC](https://discourse.julialang.org/t/mutation-of-a-scalar-variable/63798/11 "2021-06-30T06:38:01Z")

</div>

Thank you!
