Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Is it possible to name slices of arrays as functions?

$
0
0

@peterj wrote:

A common use case for me, and lots of people I would think, is to have large systems of DEs such as

dx_i / dt = …
dy_i / dt = …

where i \in [1, n] and x_i, y_i represents the ith class of x, y respectively.

Usually I define the state in these ODEs as one big vector of length 2n, like

const n = 100 #some number of dimensions
system_state = zeros(2*n)
system_state[1:n] #these are the x_i
 
system_state[n+1:2*n] #these are the y_i

Then the ODE function is something like this, with all the operations broadcasted

function f(du,u,p,t)
     x = @view u[1:n]
     y = @view u[n+1:2*n]
     du[1:n] .= -1 .* x
     du[n+1:2*n] .= x .* y
end

This works, but I end up having lots of slices around my code, which can be difficult to read.
I was hoping there was a zero-cost way of writing a shorthand function like

x(state) = @view state[1:n]

which I can then use to modify the state exactly like the original slice. However this doesn’t seem to work, I get the error “invalid assignment location”.

Is there a way to do this in Julia?

Thanks

Posts: 6

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles