Symbolic array pre-allocation

7 次查看(过去 30 天)
Symbolic array pre-allocation only allocates pointers as I understood it from reading the forum. This is causing a major slowdown in code we are looking at
a=sym(zeros(50000,1));
Filling the area with relatively simple expression e.g. '3+sqrt(5)/sqrt(7)' a serious slowdown is noted around the 9000th index.
Would it be possible to allocate memory by defining the sym array (in the definition) with large expressions '3+sqrt(1)+sqrt(2)+sqrt(3)+sqrt(4)+sqrt(5)+sqrt(6)+sqrt(7)' for all elements 1:50000, so that effectively a memory allocation is forced at the definition step. Is this possible in the definition of sym? I tried repmat, but didn't get it too work.
  2 个评论
Siddharth Sundar
Siddharth Sundar 2014-10-29
- Can you check the output of the memory command before and after the 9000th iteration of the for loop you are to define these expressions? - What is the expression you are using in the 9000th iteration?
lvn
lvn 2014-10-29
Here is a minimal examples that reproduces the behavior (and answers your questions). Thanks for trying to help!
list1=sym(zeros(50000,1));
for a=1:50000
tic
list1(a)=sym('pi+1+1');
z=toc;
[uV, sV] = memory;
fprintf('Iteration: %d, Time: %f, Memory available: %f\n', a, z, sV.PhysicalMemory.Available);
end
% ...
% Iteration: 8255, Time: 0.002197, Memory available: 5797949440.000000
% Iteration: 8256, Time: 0.001595, Memory available: 5797949440.000000
% Iteration: 8257, Time: 0.001827, Memory available: 5797949440.000000
% Iteration: 8258, Time: 0.002016, Memory available: 5797949440.000000
% Iteration: 8259, Time: 0.001667, Memory available: 5797949440.000000
% Iteration: 8260, Time: 0.001540, Memory available: 5797949440.000000
% Iteration: 8261, Time: 0.001582, Memory available: 5797949440.000000
% Iteration: 8262, Time: 0.001593, Memory available: 5797949440.000000
% Iteration: 8263, Time: 0.001547, Memory available: 5797949440.000000
% Iteration: 8264, Time: 0.001544, Memory available: 5797949440.000000
% Iteration: 8265, Time: 0.034461, Memory available: 5797949440.000000 <--- Factor 30 slower!!
% Iteration: 8266, Time: 0.030669, Memory available: 5797949440.000000
% Iteration: 8267, Time: 0.030971, Memory available: 5797949440.000000
% Iteration: 8268, Time: 0.031167, Memory available: 5797949440.000000
% Iteration: 8269, Time: 0.030904, Memory available: 5797949440.000000
% Iteration: 8270, Time: 0.031072, Memory available: 5797949440.000000

请先登录,再进行评论。

采纳的回答

Stefan Wehmeier
Stefan Wehmeier 2014-10-30
My question is in which form do you have the 50000 entries you want to fill in. As strings in a cell array A? Then
B = cellfun(@sym, A, 'UniformOutput', false);
list1 = [B{:}];
is better. Or really 50000 times the same string? Then avoid calling sym(..) on it in a loop.
All sym vectors are internally vectors of pointers. You cannot pre-allocate the memory these pointers point to ...

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by