I am porting code from Matlab to Julia, and I am having trouble finding an efficient way of adding vectors that differ in length. Here are the essential lines, from inside a recursive function:
% Matlab code
t1 = recursive_function(a+1,b-1,c,d,params);
t2 = recursive_function(a ,b-1,c,d,params); % numel(t2)==numel(t1)-1
result = t1 + [params.AB*t2 0]; % one example
result = t1 + [0 params.AB*t2]; % another example
result
is what gets returned by the function. I translated this to Julia in the obvious (and likely overly naĂŻve) way
# Julia code
t1 = recursive_function(a+1,b-1,c,d,params)
t2 = recursive_function(a ,b-1,c,d,params) # numel(t2)==numel(t1)-1
result = t1 + push!(params.AB*t2,0.0) # one example
result = t1 + unshift!(0.0,params.AB*t2) # another example
With this, I get a lot of memory allocations. @code_warntype
doesn’t highlight any type instabilities.
Is there a more efficient way to calculate result
?