# \#broadcast

**URL:** https://discourse.julialang.org/tag/broadcast/108.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [Idiomatic evaluate for scalar, allocate and broadcast for array?](https://discourse.julialang.org/t/idiomatic-evaluate-for-scalar-allocate-and-broadcast-for-array/135147)

<div class="topic-metadata">

**Author:** [@bremez](https://discourse.julialang.org/u/bremez)\
**Replies:** 7\
**Last updated:** [January 21, 2026, 7:01am UTC](https://discourse.julialang.org/t/idiomatic-evaluate-for-scalar-allocate-and-broadcast-for-array/135147 "2026-01-21T07:01:58Z")

</div>

In several cases I’ve come across the following pattern: function f(x; kwargs...) #intense pre-computation that depend on kwargs but not x result = 0.0 \* x; # one way to preallocate for e.g. x::Vector, but not for scala…

---

## [Keyword arguments not allowed on left-hand side of broadcast assignments?](https://discourse.julialang.org/t/keyword-arguments-not-allowed-on-left-hand-side-of-broadcast-assignments/133920)

<div class="topic-metadata">

**Author:** [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Replies:** 1\
**Last updated:** [November 17, 2025, 3:18am UTC](https://discourse.julialang.org/t/keyword-arguments-not-allowed-on-left-hand-side-of-broadcast-assignments/133920 "2025-11-17T03:18:44Z")

</div>

Broadcasting with assignment works with a function call on the left-hand side. However, function calls with keyword arguments do not work for some reason: julia\> f(v; a = 0) = copy(v) f (generic function with 1 method) …

---

## [Is it safe to broadcast \`setindex!\` with views of the same array?](https://discourse.julialang.org/t/is-it-safe-to-broadcast-setindex-with-views-of-the-same-array/133620)

<div class="topic-metadata">

**Author:** [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Replies:** 4\
**Last updated:** [November 3, 2025, 4:53pm UTC](https://discourse.julialang.org/t/is-it-safe-to-broadcast-setindex-with-views-of-the-same-array/133620 "2025-11-03T16:53:25Z")

</div>

I tried to find out looking here and through the documentation, but I couldn’t find a concrete answer so far: Is it safe to re-order array elements in-place using a view into the same array, like so: julia\> A = rand(10…

---

## [Custom (NumPy style) broadcasting rule that avoids iterating over elements (for GPU-acceleration)](https://discourse.julialang.org/t/custom-numpy-style-broadcasting-rule-that-avoids-iterating-over-elements-for-gpu-acceleration/131515)

<div class="topic-metadata">

**Author:** [@TimHargreaves](https://discourse.julialang.org/u/TimHargreaves)\
**Replies:** 10\
**Last updated:** [August 24, 2025, 7:48pm UTC](https://discourse.julialang.org/t/custom-numpy-style-broadcasting-rule-that-avoids-iterating-over-elements-for-gpu-acceleration/131515 "2025-08-24T19:48:57Z")

</div>

Minimal Example As a minimal example, suppose that I have the following two structs defined, struct Foo{T, M\<:AbstractMatrix{T}} A::M B::M end struct Bar{T, M\<:AbstractMatrix{T}} C::M foo::Foo{T, M} end…

---

## [Writing a function that also works broadcasts over DataFrames](https://discourse.julialang.org/t/writing-a-function-that-also-works-broadcasts-over-dataframes/131675)

<div class="topic-metadata">

**Author:** [@freeman](https://discourse.julialang.org/u/freeman)\
**Replies:** 16\
**Last updated:** [August 18, 2025, 9:07pm UTC](https://discourse.julialang.org/t/writing-a-function-that-also-works-broadcasts-over-dataframes/131675 "2025-08-18T21:07:04Z")

</div>

I love broadcasting over arrays. I love that in Julia I can define func(x) where x is e.g. a namedtuple and then call it on func.(x) where x is a vector of namedtuples. However… I’m having difficulty extending this to …

---

## [Broadcast a function returning multiple values](https://discourse.julialang.org/t/broadcast-a-function-returning-multiple-values/131314)

<div class="topic-metadata">

**Author:** [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Replies:** 15\
**Last updated:** [August 5, 2025, 7:49am UTC](https://discourse.julialang.org/t/broadcast-a-function-returning-multiple-values/131314 "2025-08-05T07:49:53Z")

</div>

If I understand it correctly, a function that looks like returning multiple values actually returns a Tuple of the values and therefore the result of broadcasting on the function is an array of the Tuples: function f1(x…

---

## [why doesn't \`@.\` always broadcast?](https://discourse.julialang.org/t/why-doesnt-always-broadcast/130892)

<div class="topic-metadata">

**Author:** [@rokke](https://discourse.julialang.org/u/rokke)\
**Replies:** 12\
**Last updated:** [July 21, 2025, 3:00pm UTC](https://discourse.julialang.org/t/why-doesnt-always-broadcast/130892 "2025-07-21T15:00:06Z")

</div>

some examples: julia\> @. x-\>x\[1\] #1 (generic function with 1 method) julia\> ans( \[\[1,2,3\],\[9,8,7\]\] ) 3-element Vector{Int64}: 1 2 3 julia\> @. x-\>getindex(x,1) #3 (generic function with 1 method) julia\> ans( \[\[1,2,…

---

## [What is interaction between F.() broadcasting and keyword args?](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648)

<div class="topic-metadata">

**Author:** [@ndbecker](https://discourse.julialang.org/u/ndbecker)\
**Replies:** 13\
**Last updated:** [June 4, 2025, 12:44pm UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648 "2025-06-04T12:44:49Z")

</div>

How does F.(a, b; c=d) work with respect to broadcasting? I understand that the arguments a, b will be broadcast. What about the keyword argument?

---

## [Double broadcast](https://discourse.julialang.org/t/double-broadcast/127930)

<div class="topic-metadata">

**Author:** [@atteson](https://discourse.julialang.org/u/atteson)\
**Replies:** 4\
**Last updated:** [April 10, 2025, 5:17pm UTC](https://discourse.julialang.org/t/double-broadcast/127930 "2025-04-10T17:17:52Z")

</div>

I frequently find myself wishing to do a double broadcast, e.g.: f = x-\>x+1 (v -\> f.(v)).( \[\[1,2,3\], \[4,5,6\]\]) Is this the most concise way to do it?

---

## [In-place broadcasting for a specific function](https://discourse.julialang.org/t/in-place-broadcasting-for-a-specific-function/124680)

<div class="topic-metadata">

**Author:** [@arnaud-ma](https://discourse.julialang.org/u/arnaud-ma)\
**Replies:** 8\
**Last updated:** [January 12, 2025, 3:40am UTC](https://discourse.julialang.org/t/in-place-broadcasting-for-a-specific-function/124680 "2025-01-12T03:40:03Z")

</div>

My code is as follows: function get\_foo(x, y, args...) # specific algorithm that modifies x and y... return x + y end function gen\_foo!(A::AbstractMatrix, args...) # is equivalent to: for coord in Carte…

---

## [AbstractArrayStyle{Any} versus AbstractArrayStyle{N} broadcast style](https://discourse.julialang.org/t/abstractarraystyle-any-versus-abstractarraystyle-n-broadcast-style/124526)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 1\
**Last updated:** [January 8, 2025, 8:40pm UTC](https://discourse.julialang.org/t/abstractarraystyle-any-versus-abstractarraystyle-n-broadcast-style/124526 "2025-01-08T20:40:33Z")

</div>

Suppose a package defines an array type SomeArray which, like Array, supports arbitrary dimensionality: struct SomeArray{N} \<: AbstractArray{Float32, N} end # the element type doesn't matter here, it's merely an exampl…

---

## [Broadcasting \`$\` as a binary operator](https://discourse.julialang.org/t/broadcasting-as-a-binary-operator/121229)

<div class="topic-metadata">

**Author:** [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Replies:** 7\
**Last updated:** [October 13, 2024, 12:03am UTC](https://discourse.julialang.org/t/broadcasting-as-a-binary-operator/121229 "2024-10-13T00:03:44Z")

</div>

It’s possible to define $ as a binary operator, but it turns out you can’t use it with broadcasting: julia\> ($)(x, y) = x + y $ (generic function with 1 method) julia\> 1 $ 2 3 julia\> \[1, 2\] .$ \[3, 4\] ERROR: ParseError…

---

## [Broadcast hangs after successful execution over all array elements](https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193)

<div class="topic-metadata">

**Author:** [@Bananthahally](https://discourse.julialang.org/u/Bananthahally)\
**Replies:** 11\
**Last updated:** [September 10, 2024, 4:50am UTC](https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193 "2024-09-10T04:50:12Z")

</div>

When I call the function INSIDE\_WHICH\_INTVAL below, it correctly returns IVL\_NUM for input of type INTY :: Float64. When I broadcast the function to cover all elements of an array INTY\_ARRAY also, as INSIDE\_WHICH\_INTVAL.…

---

## [OffsetArray of size 1 from elementwise multiplication](https://discourse.julialang.org/t/offsetarray-of-size-1-from-elementwise-multiplication/119135)

<div class="topic-metadata">

**Author:** [@liamh](https://discourse.julialang.org/u/liamh)\
**Replies:** 3\
**Last updated:** [September 9, 2024, 2:57pm UTC](https://discourse.julialang.org/t/offsetarray-of-size-1-from-elementwise-multiplication/119135 "2024-09-09T14:57:53Z")

</div>

When I elementwise multiply an offset array by a regular array, I get the result I expect - the resulting array is offset properly - except when the size is 1. In this example, the input array dimension is 6:10, and the…

---

## [Broadcasting on views](https://discourse.julialang.org/t/broadcasting-on-views/118459)

<div class="topic-metadata">

**Author:** [@pratikvn](https://discourse.julialang.org/u/pratikvn)\
**Replies:** 2\
**Last updated:** [August 22, 2024, 11:22am UTC](https://discourse.julialang.org/t/broadcasting-on-views/118459 "2024-08-22T11:22:45Z")

</div>

Hello, I would like to perform a broadcast operation on views with indices stored in a struct. I am not sure how exactly to acheive this with Julia syntax. Essentially I have a struct like so: struct Foo id::Inte…

---

## [Performance of loops](https://discourse.julialang.org/t/performance-of-loops/118273)

<div class="topic-metadata">

**Author:** [@Matthijs\_1971](https://discourse.julialang.org/u/Matthijs_1971)\
**Replies:** 30\
**Last updated:** [August 19, 2024, 8:20pm UTC](https://discourse.julialang.org/t/performance-of-loops/118273 "2024-08-19T20:20:45Z")

</div>

Julia has several ways to define loops or repeat code, each with advantages and disadvantages. I am looking at the performance of some also inspired by previous posts on this forum. #!/usr/bin/env julia # https://disco…

---

## [Multithreaded broadcast on a grouped DimensionalData array](https://discourse.julialang.org/t/multithreaded-broadcast-on-a-grouped-dimensionaldata-array/117607)

<div class="topic-metadata">

**Author:** [@aasdelat](https://discourse.julialang.org/u/aasdelat)\
**Replies:** 33\
**Last updated:** [August 6, 2024, 4:04pm UTC](https://discourse.julialang.org/t/multithreaded-broadcast-on-a-grouped-dimensionaldata-array/117607 "2024-08-06T16:04:30Z")

</div>

Hello: I am using the Rasters.jl package to read and work with data on a spatial and a time dimension (two dimensions). So it is a Raster with two dimensions. I have gropued the raster by means of the groupby() functio…

---

## [Vectorizing a summation](https://discourse.julialang.org/t/vectorizing-a-summation/117435)

<div class="topic-metadata">

**Author:** [@gkeee](https://discourse.julialang.org/u/gkeee)\
**Replies:** 5\
**Last updated:** [July 24, 2024, 7:31pm UTC](https://discourse.julialang.org/t/vectorizing-a-summation/117435 "2024-07-24T19:31:15Z")

</div>

term = 0 for k in 1:N term\_inner = 0 for p in 0:m term\_inner += binomial(m, p) \* c\[k\]^p \*f\[k\]^(m - p) \* integral\_Ik\_recursive(p, γ) end term += exp(im \* 2π \* γ \* (k - 1)) \* term\_inner end I want …

---

## [Scaling a sparse matrix row-wise and column-wise too slow](https://discourse.julialang.org/t/scaling-a-sparse-matrix-row-wise-and-column-wise-too-slow/115956)

<div class="topic-metadata">

**Author:** [@pitsianis](https://discourse.julialang.org/u/pitsianis)\
**Replies:** 20\
**Last updated:** [June 23, 2024, 9:17pm UTC](https://discourse.julialang.org/t/scaling-a-sparse-matrix-row-wise-and-column-wise-too-slow/115956 "2024-06-23T21:17:22Z")

</div>

I need to scale a sparse matrix row-wise and column-wise. That is S\[i,j\] = r\[i\] \* A\[i,j\] \* c\[j\] If I write it the way you’d see it in a math book, S = spdiagm(r) \* A \* spdiagm(c) the expression takes 8x times more th…

---

## [String broadcasting](https://discourse.julialang.org/t/string-broadcasting/115621)

<div class="topic-metadata">

**Author:** [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Replies:** 5\
**Last updated:** [June 18, 2024, 9:54pm UTC](https://discourse.julialang.org/t/string-broadcasting/115621 "2024-06-18T21:54:36Z")

</div>

So I was following a tutorial, but I got confused when they showed the following code: vec(string.(\[“x”,“y”\],\[1 2 3 4\])) First, how am I supposed to infer the broadcasting of string given a vector and a matrix will re…

---

## [Broadcasting functions with multiple arguments in same dimension using Mapslices](https://discourse.julialang.org/t/broadcasting-functions-with-multiple-arguments-in-same-dimension-using-mapslices/114586)

<div class="topic-metadata">

**Author:** [@chihlun](https://discourse.julialang.org/u/chihlun)\
**Replies:** 14\
**Last updated:** [May 22, 2024, 11:41pm UTC](https://discourse.julialang.org/t/broadcasting-functions-with-multiple-arguments-in-same-dimension-using-mapslices/114586 "2024-05-22T23:41:42Z")

</div>

Hi everyone, I have a simple code as shown below. Both a and y are 4D matrices. I want the function f(x, y) to operate on the third dimension, summing the x matrix along the 3rd dimension, summing the y matrix along the…

---

## [At dot of function of five arguments against five vectors to five dimensional array](https://discourse.julialang.org/t/at-dot-of-function-of-five-arguments-against-five-vectors-to-five-dimensional-array/114146)

<div class="topic-metadata">

**Author:** [@PeterSP](https://discourse.julialang.org/u/PeterSP)\
**Replies:** 6\
**Last updated:** [May 12, 2024, 6:13pm UTC](https://discourse.julialang.org/t/at-dot-of-function-of-five-arguments-against-five-vectors-to-five-dimensional-array/114146 "2024-05-12T18:13:25Z")

</div>

I’ve seen many examples that generate a two dimensional array (matrix) from a function of two arguments against two vectors: x = 1:100 y = 1:100 f(x,y) = x\*y z = @. f(x', y) I want to do something analagous, but five-d…

---

## [Allocations in broadcasted assignment (StackOverflow question)](https://discourse.julialang.org/t/allocations-in-broadcasted-assignment-stackoverflow-question/109680)

<div class="topic-metadata">

**Author:** [@bert](https://discourse.julialang.org/u/bert)\
**Replies:** 2\
**Last updated:** [February 4, 2024, 7:21am UTC](https://discourse.julialang.org/t/allocations-in-broadcasted-assignment-stackoverflow-question/109680 "2024-02-04T07:21:49Z")

</div>

I came across this StackOverflow post and am wondering what the solution is. I have played around with the example in this post (copied below) and, like the author, cannot get rid of the allocation in d’s broadcast assig…

---

## [Why does profiling suggest that integer comparison dominates broadcasted assignment?](https://discourse.julialang.org/t/why-does-profiling-suggest-that-integer-comparison-dominates-broadcasted-assignment/109606)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 4\
**Last updated:** [February 3, 2024, 2:47am UTC](https://discourse.julialang.org/t/why-does-profiling-suggest-that-integer-comparison-dominates-broadcasted-assignment/109606 "2024-02-03T02:47:53Z")

</div>

julia\> a = rand(40000,4000); b = rand(40000,4000); julia\> Profile.clear() julia\> @bprofile $a\[1:end, 1:end\] .= $b BenchmarkTools.Trial: 9 samples with 1 evaluation. Range (min … max): 571.819 ms … 586.555 ms ┊ GC (m…

---

## [Access each element in the broadcast operation](https://discourse.julialang.org/t/access-each-element-in-the-broadcast-operation/108728)

<div class="topic-metadata">

**Author:** [@Afonso\_Britto](https://discourse.julialang.org/u/Afonso_Britto)\
**Replies:** 6\
**Last updated:** [January 15, 2024, 8:11am UTC](https://discourse.julialang.org/t/access-each-element-in-the-broadcast-operation/108728 "2024-01-15T08:11:03Z")

</div>

Is there anyway I can acess the current element I’m iterating through when I use a broadcast operation? I would want to make this code in just one line, so, for every pixel I’m iterating in my image I would want to perfo…

---

## [Substantial increase in time in copying an instantiated broadcasted object vs a non-instantiated one](https://discourse.julialang.org/t/substantial-increase-in-time-in-copying-an-instantiated-broadcasted-object-vs-a-non-instantiated-one/108673)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 2\
**Last updated:** [January 11, 2024, 11:49am UTC](https://discourse.julialang.org/t/substantial-increase-in-time-in-copying-an-instantiated-broadcasted-object-vs-a-non-instantiated-one/108673 "2024-01-11T11:49:58Z")

</div>

I’m trying to understand the difference in performance here: julia\> using LinearAlgebra julia\> n=40; U = UpperTriangular(rand(n,n)); C = similar(U); B = Broadcast.broadcasted(\*, 2.0, U); julia\> @btime copyto!($C, $B);…

---

## [Efficient Broadcasting in Julia: CartesianIndex vs. Reshape](https://discourse.julialang.org/t/efficient-broadcasting-in-julia-cartesianindex-vs-reshape/106929)

<div class="topic-metadata">

**Author:** [@swanchristmas](https://discourse.julialang.org/u/swanchristmas)\
**Replies:** 1\
**Last updated:** [November 30, 2023, 8:12am UTC](https://discourse.julialang.org/t/efficient-broadcasting-in-julia-cartesianindex-vs-reshape/106929 "2023-11-30T08:12:22Z")

</div>

I came across a discussion post on using \[CartesianIndex()\] for singleton insertion in Julia arrays. Intrigued by potential performance differences, I conducted a benchmark comparing it with the reshape method. Benchmar…

---

## [Why ~ can not broadcast?](https://discourse.julialang.org/t/why-can-not-broadcast/106290)

<div class="topic-metadata">

**Author:** [@zhpf0530](https://discourse.julialang.org/u/zhpf0530)\
**Replies:** 2\
**Last updated:** [November 16, 2023, 3:16am UTC](https://discourse.julialang.org/t/why-can-not-broadcast/106290 "2023-11-16T03:16:00Z")

</div>

why ~ can not broadcast? a=randn(10) b=a.\>0 ~.b is wrong ~b is wrong map(~,b) is right.

---

## [Broadcasting call to a vector-valued function](https://discourse.julialang.org/t/broadcasting-call-to-a-vector-valued-function/106056)

<div class="topic-metadata">

**Author:** [@elenev](https://discourse.julialang.org/u/elenev)\
**Replies:** 4\
**Last updated:** [November 11, 2023, 1:55am UTC](https://discourse.julialang.org/t/broadcasting-call-to-a-vector-valued-function/106056 "2023-11-11T01:55:38Z")

</div>

What is the most julian way to broadcast a call to a vector-valued function? For example, take the following function. function return\_vec(x) return \[x, x^2\] end I would like to collect outputs for each x in 1:15 …

---

## [How can I make an in-place swap function?](https://discourse.julialang.org/t/how-can-i-make-an-in-place-swap-function/102489)

<div class="topic-metadata">

**Author:** [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Replies:** 9\
**Last updated:** [August 4, 2023, 8:30pm UTC](https://discourse.julialang.org/t/how-can-i-make-an-in-place-swap-function/102489 "2023-08-04T20:30:58Z")

</div>

Suppose I have two arrays A, B of different sizes. Then A .+ B broadcasts them to a common size (if possible) and sums them. Suppose A here is “larger” (in the sense that all(size(B) .== size(A) .|| size(B) .== 1) is tru…

[Next page](https://discourse.julialang.org/tag/broadcast/108.md?match_all_tags=true&page=1&tags%5B%5D=broadcast)
