GCOMPILE support for GFOR? - matlab

I stumbled across this problem while working with Jacket.
I use a compiled function (compiled with gcompile) within a gfor loop. This is meant to be supported as far as I know: http://wiki.accelereyes.com/wiki/index.php/GCOMPILE
But I observed that while the uncompiled function delivers the correct results, the compiled function gives the same output for all the gfor-iterations:
%================
% function[C] = test(A,B)
% C = A+B;
% end
%================
testing = gcompile('test.m');
A = gdouble(1:1:10);
B = gdouble(2:2:20);
C1 = gzeros(10,1);
C2 = gzeros(10,1);
gfor l=1:10
C1(l) = test(A(l),B(l));
C2(l) = testing(A(l),B(l));
gend
The output is:
C1 = [ 3,6,9,12,15,18,21,24,27,30]
(correct result)
C2 = [ 3,3,3,3,3,3,3,3,3,3]
Can you verify/rebut my results?
What am I doing wrong?
Cheers,
Angela

I was able to reproduce this behavior by running Jacket on MATLAB. It appears that gcompile does not work over GFOR as it should, and the documentation was in error. Sorry about that.

Related

How can I determine disk space in MATLAB

Is there any function in MATLAB that determine free disk space? I have made a temporal function that uses MS-DOS dir command and parses the last line of its output. I think it's working as expected but I guess (1) it won't work in other systems (OS X, Linux, Unix, etx.) and (2) can also fail in different Windows versions. Perhaps someone could improve it to make it more generic? Thanks
The code:
function out = freediskspace
[~,d] = dos('dir');
C = textscan(d,'%s','Delimiter','\n'); C = C{1}{end};
C = strrep(C,',','');
r = regexp(C,'\d+','match');
out = str2double(r{2});
end
You can use a Java call (this works on both Linux and Windows - I have not checked OSX but it should be fine).
function free = getFreeSpace(path)
if nargin < 1 || isempty(path)
path= '.';
end
free = java.io.File(path).getFreeSpace();
end
For example,
>> f = getFreeSpace('C:\')
f =
3.9338e+11

Use of if statement in for loop in this code

What does the statement for if=ilow:ihigh mean in this program?
function [d]=for_taup(m,dt,h,q,N,flow,fhigh);
nt= max(size(m));
nh = max(size(h));
M = fft(m,[],1);
D = zeros(nt,nh);
i = sqrt(-1);
ilow = floor(flow*dt*nt)+1; if ilow<1; ilow=1;end;
ihigh = floor(fhigh*dt*nt)+1;
if ihigh>floor(nt/2)+1; ihigh=floor(nt/2)+1;end
for if=ilow:ihigh
f = 2.*pi*(if-1)/nt/dt;
L = exp(i*f*(h.^N)’*q);
x = M(if,:)’;
y = L * x;
D(if,:) = y’;
D(nt+2-if,:) = conj(y)’;
end
D(nt/2+1,:) = zeros(1,nh);
d = real(ifft(D,[],1));
return;
if is used as a variable name. I am surprised that this does not raise a syntax error: most languages would forbid the use of "reserved" keywords. Maybe it would be a good idea to replace if with a different name in order to clarify your code and avoid confusion.
As far as MATLAB is concerned, this code doesn't really mean anything, because it's just a syntax error. if is reserved keyword, and you can't create a variable called if. As such, it just instantly errors and won't run.
You should probably replace all occurrences of the variable if (although not the keyword if in lines 8 and 10) with some other variable name. Avoid i, since you're using that as the imaginary unit.

Matlab: how to sum fields of two equivalent structures?

I am working in Matlab. I have defined:
a(1).x=1;
a(1).y=2;
a(1).z.w=3;
a(2).x=4;
a(2).y=5;
a(2).z.w=6;
I am now trying to add the fields in the two structures a(1) and a(2) such that I get:
c.x = 5;
c.y = 7;
c.z.w = 9;
Any idea how I can do this in an elegant way? Note that in the original problem the structures have many more fields (around 50).
Thank you very much in advance!
José
Here is a solution, for any depth of struct
The code of the script (or MATLAB command)
a(1).x=1;
a(1).y=2;
a(1).z.w=3;
a(2).x=4;
a(2).y=5;
a(2).z.w=6;
c=a(1);
c = returnStruct(c, a(2));
%{
you can also sum any amount of structs
for i=2:length(a)
c=returnStruct(c, a(i));
end
%}
with the recursive function
function rs = returnStruct(s,a)
fn = fieldnames(s);
for i=1:length(fn)
if isstruct(s.(fn{i}))
s.(fn{i}) = returnStruct(s.(fn{i}), a.(fn{i}));
else
s.(fn{i}) = s.(fn{i})+a.(fn{i});
end
end
rs = s;
end
I tested it for deeper levels of structs and it worked perfectly. Maybe, you have to adapt it slightly for your case, but this should be the way to go.
Unfortunately, any function like struct2cell only converts the first level, so you need something else.
If the deepest substructure level is 2 then this code will work:
fields=fieldnames(a);
for i=1:numel(fields)
if isstruct(a(1).(fields{i}))
fields2=fieldnames(a(1).(fields{i}));
for j=1:numel(fields2)
a(3).(fields{i}).(fields2{j})= a(1).(fields{i}).(fields2{j})+a(2).(fields{i}).(fields2{j});
end
else
a(3).(fields{i})=a(1).(fields{i})+a(2).(fields{i});
end
end
You can have a simple recursive solution
function r = sumfields(s)
if isstruct(s)
for f = fieldnames(s).'
r.(f{1}) = sumfields([s.(f{1})]);
end
else
r = sum(s);
end
end

If and elseif, only the first line work?

L=1; Nx=51; PeriodicFlag=1; T=15; Nt=51;
spacedef='Pade6FirstDeriv'; casedef='LinearAdvection';
if (spacedef == 'Pade6FirstDeriv')
D1 = sparse(Pade6(Nx,dx,PeriodicFlag));
elseif (spacedef == 'Upwind3FirstDeriv')
D1 = sparse(Upwind3(Nx,dx,PeriodicFlag));
elseif (spacedef == 'Central4FirstDeriv')
D1 = sparse(Central4(Nx,dx,PeriodicFlag));
elseif (spacedef == 'Central2FirstDeriv')
D1 = sparse(Central2(Nx,dx,PeriodicFlag));
else
error(sprintf('Unknown spacedef = %s',spacedef));
end
In the above code, the if section is a small segment from a function I've constructed. I'm trying to get the function to know which methods to use based on my input (spacedef). Central2, Central4, Upwind3, and Pade6 are other functions I've written. The weird thing is that when spacedef =/= to 'Pade6FirstDeriv', I would get an error stating Error using ==, Matrix dimensions must agree. I've tried swapping the order of the if loop (by placing Central4, Central2, Pade6, and Upwind3 in the first line of the loop), and it seems like only the top line of the loop will work (the elseifs are not working). I'd greatly appreciate it if anybody can help me out. Thanks!
As has been noted in the comments, this is a common error when people first start comparing strings in MATLAB, and strcmp or strcmpi is generally the solution.
However, one solution the questions links in the comments don't present, and a solution I think looks much nicer, is the switch statement:
switch (spacedef)
case('Pade6FirstDeriv')
D1 = sparse(Pade6(Nx,dx,PeriodicFlag));
case('Upwind3FirstDeriv')
D1 = sparse(Upwind3(Nx,dx,PeriodicFlag));
case('Central4FirstDeriv')
D1 = sparse(Central4(Nx,dx,PeriodicFlag));
case('Central2FirstDeriv')
D1 = sparse(Central2(Nx,dx,PeriodicFlag));
otherwise
error(sprintf('Unknown spacedef = %s',spacedef));
end
Note: if I expect others to use my code with string comparisons, I usually lower the input such that the comparison is case-insensitive, although I have not done that here.

Call Eviews from Matlab

I am trying to call Eviews (8, 32bit) from Matlab (2013a, 32bit), but without success so far. I used the following code in Matlab:
clear all; clc;
hm = actxserver('EViews.Manager.8')
hm = COM.Eviews_Manager
h = hm.GetApplication(0)
h = Interface.EViews_8.0_Type_Library.IApplication
h.invoke
h.Show()
h.Run('wfcreate comEV u 4')
h.Run('genr x = #obsid')
I get the following error:
>> h = hm.GetApplication(0)
No appropriate method, property, or field GetApplication for class
COM.EViews_Manager_8.
Could anyone please help? Thanks in advance.
At the very least, you could write your program in EViews and then call it from Matlab* using the shell:
system(['"C:\Program Files\EViews 9\EViews9_x64.exe" "C:\Users\me\Documents\foo.prg"'])
*or Stata, R, Python, VBA, etc.
The line of code
h = Interface.EViews_8.0_Type_Library.IApplication
is not a command, is the output from the above line of your code
(h = hm.GetApplication(0)).
Just remove it from your script. I think this is the problem.