juliastructuremultiplication# Simple command to multiply all values of a dictionary by a float

In Julia, you can easily multiply all values of a `Vector`

, `Matrix`

or `Array`

`a`

by a `Float64`

, say 2, by:

```
a=ones(3,3)
a*=2
```

I wanted to know if this is also easily achievable for dictionaries, for instance, `Dictionnary{Int,Float64}`

or `Dictionnary{Tuple{Int,Int},Float64}`

I know it can be done by iterating with for loops on keys and values but I want to do it "in place" like `dict*=2`

. Is it possible?

Solution

This in-place `map!`

might be 20X faster than `replace`

.

```
map!(x->2x, values(d))
```

Testing:

```
julia> @benchmark map!(x->2x, values(d)) setup=(d = Dict(1:100 .=> rand(100)))
BenchmarkTools.Trial: 10000 samples with 985 evaluations.
Range (min … max): 59.391 ns … 154.315 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 60.508 ns ┊ GC (median): 0.00%
Time (mean ± σ): 61.706 ns ± 4.912 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
█
▃██▆▄▄▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂ ▂
59.4 ns Histogram: frequency by time 86.9 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
```

vs.

```
julia> @benchmark d_mult = replace(kv -> kv[1] => kv[2]*2, d) setup=(d = Dict(1:100 .=> rand(100)))
BenchmarkTools.Trial: 10000 samples with 30 evaluations.
Range (min … max): 916.667 ns … 274.520 μs ┊ GC (min … max): 0.00% … 99.13%
Time (median): 1.153 μs ┊ GC (median): 0.00%
Time (mean ± σ): 3.052 μs ± 11.919 μs ┊ GC (mean ± σ): 21.27% ± 5.50%
▆█▅▄▁ ▁▂▂▃▂▄▇▆▃▁ ▂
█████▇▇▆▄▄▁▁▄▁▄▅▅▃▃▁▃▁▁███████████▇▇▆▆▅▅▄▄▅▆▅▅▆▄▄▅▄▃▄▄▅▅▅▆▆▆█ █
917 ns Histogram: log(frequency) by time 7.68 μs <
Memory estimate: 4.72 KiB, allocs estimate: 5.
```

- Julia: is there a function to obtain the version number of a package?
- Get permutation of one array based on the size ordering of the other array
- Making Julia show different plots in different windows
- How to get equally scaled axes with Plots in Julia
- Scatter plot of two rows of a DataFrame in Julia using Plotly
- Adding constraints to jump model from dict
- Non-iterable argument to a function called by Julia `map`?
- Compute row sums and column sums efficiently in Julia
- Julia copying folder into an existing folder
- How to replace a function in Julia?
- How to force Julia to use multiple threads for matrix multiplication?
- Can Revise.jl handle `ERROR: LoadError: invalid redefinition of constant`?
- Define piecewise function with automatic broadcasting in Julia
- julia Handling time difference in dataframe
- Fast tensor-dot on sparse arrays with GPU in any programming language?
- Why do allocations occur during broadcasting assignment to a preallocated array?
- Comparing RK4 to Euler method
- How to put even numbers from matrix in separate vector in Julia?
- Number of iterations performed by a for-loop in Julia
- Can I redefine a function, but still use the old definition within the new definition?
- how do I use analytical form as gradient with ! function?
- Julia equivalent to R `as.numeric()`
- Why does Float16(1.1)-Float16(1)=Float16(0.0996)?
- Julia manual and defining an infix operator
- In Julia, how to convert a unsigned number to a signed number like in C?
- Julia Package DataFrames 1.6.1 doesn't recognize old version DataFrames 1.3.4 file
- Convert Julia DataFrame to an array of bytes for compression
- How to suppress svg output by IJulia in Jupyter notebooks
- how to get the documentation of a julia function in a vscode notebook?
- Passing command-line arguments to a Pluto Notebook