I'm getting the above error in my code, which indicates the 'while' line.
I'm trying to find the number of intersection points to a line on some gis data.
I've copied the code verbatim, the postgis code shouldn't affect the problem.
(And if I'm trying to do this in a really stupid way, please say. I'm only a beginner)
create or replace function border() returns table(x real, sum bigint) as $$
declare x real;
begin
x := -35.5724;
while x > -36.4 do
return query select x,sum(st_npoints(st_intersection(the_geom,st_setsrid(st_makeline(st_point(173.3,x),st_point(175,x)),4167)))) from auckland_numberlines;
x := x - 0.1;
end while
end
$$ language plpgsql;
while x > -36.4 do
return query select x,sum(st_npoints(st_intersection(the_geom,st_setsrid(st_makeline(st_point(173.3,x),st_point(175,x)),4167)))) from auckland_numberlines;
x := x - 0.1;
end while
should be
WHILE x > -36.4 LOOP
return query select x,sum(st_npoints(st_intersection(the_geom,st_setsrid(st_makeline(st_point(173.3,x),st_point(175,x)),4167)))) from auckland_numberlines;
x := x - 0.1;
END LOOP ;
Related
I have a small MATLAB symbolic script as following
syms z;
psi(2)=exp(2*z-exp(z))/(1-exp(-exp(z)));
psi(3)=exp(2*z-exp(z))/(1-exp(-exp(z)))*z;
psi(4)=exp(2*z-exp(z))/(1-exp(-exp(z)))*z^2;
f(1,1)=exp(2*z-exp(z))/(1-exp(-exp(z)));
for i=2:4
f(i,1)=diff(psi(i),z);
for j=2:i
f(i,j)=diff(f(i,j-1)/f(j-1,j-1),z);
end
end
given a symbolic vector psi consist of functions of z, it create a lower triangle symbolic matrix f. it works well.
I'm trying to rewrite this part in Maple, which I'm new to. I tried
psi(2) := exp(2*z-exp(z))/(1-exp(-exp(z)));
psi(3) := exp(2*z-exp(z))*z/(1-exp(-exp(z)));
psi(4) := exp(2*z-exp(z))*z^2/(1-exp(-exp(z)));
f(1, 1) := exp(2*z-exp(z))/(1-exp(-exp(z)));
for i from 2 to 4 do f(i,1):=exp(2*z-exp(z))/(1-exp(-exp(z)));
for j from 2 to i do f(i,j):=diff(f(i,j-1)/f(j-1,j-1),z);
od;
od;
something ambiguous in the "diff" line, I just select function definition. if I let it output f(4,4), it report
Error, (in f) too many levels of recursion
but it did print f(4,1).
could some one tell what's wrong? Thanks!
Your code is pretty close (and reminds me how similar these two languages are at times). The reason for the error message is that you need to declare f before you start filling it with values.
Here's one possible solution:
psi[2] := exp(2*z-exp(z))/(1-exp(-exp(z)));
psi[3] := exp(2*z-exp(z))*z/(1-exp(-exp(z)));
psi[4] := exp(2*z-exp(z))*z^2/(1-exp(-exp(z)));
f := Matrix(1..4,1..4):
f[1, 1] := exp(2*z-exp(z))/(1-exp(-exp(z))):
for i from 2 to 4 do
f[i,1] := diff(psi[i],z):
for j from 2 to i do
f[i,j] := diff(f[i,j-1]/f[j-1,j-1],z):
end do:
end do:
f;
Note here that I declare f to be a 4x4 Matrix before I start filling it. Also, here the [] notation is used for specifying indices.
Another option which may scale better for larger problems is to grow your data structure for f as you add values to it. Here we start with a 1x1 Array and add values to it.
psi[2] := exp(2*z-exp(z))/(1-exp(-exp(z)));
psi[3] := exp(2*z-exp(z))*z/(1-exp(-exp(z)));
psi[4] := exp(2*z-exp(z))*z^2/(1-exp(-exp(z)));
f:=Array(1..1,1..1):
f(1, 1) := exp(2*z-exp(z))/(1-exp(-exp(z))):
for i from 2 to 4 do
f(i,1):=diff(psi[i],z):
for j from 2 to i do
f(i,j):=diff(f[i,j-1]/f[j-1,j-1],z):
end do:
end do:
f;
Here you'll notice that we are using the () notation for Array indices at time of creation. If you use an Array for storage, this is one technique that allows for you to grow the Array as you add values.
Now in both cases you can also note that I've used [] to index a term that already exists; square brackets are the default notation in Maple for specifying indices in a data structure.
Also note that I've suppressed output in each loop using the : operator; this way you can just echo back the resulting Matrix f at the end.
How to convert this part of Matlab code to Delphi?
for i=1:popsize
fi=rand(1,dimension); % Generate a vector of uniform random numbers
p=pbest(i,:);
pbest(i,:)=x(i,:);
end
My code:
for i:= 1 to popsize do
begin
fi:= // which function generates vector of uniform random numbers in Delphi?
for k :=1 to popsize do
begin
p:=pbest(i,k);
pbest(i,k):=x(i,k);
end;
end;
You can call Random function to generate a uniformly distributed random value. Calling Randomize once makes Random generate different values in each run.
var
fi: array of Double;
J: Integer;
begin
Randomize;
for J := 0 to dimension - 1 do
fi[J] := Random;
end;
It seems that it is at least not encouraged to write Modelica functions with connectors as arguments. I get a warning if I try it.
Assume I have a connector
connector con
Real x;
Real y;
end con;
a record
record rec
Real x;
Real y;
end rec;
and a function
function f
input rec r[:];
output Real z;
algorithm
...
end f;
Given an array of connectors, i.e. con c[N], how can I convert it into an array of records rec?
One approach would be to use a function
function convert
input Integer N;
input Real x[N];
input Real y[N];
output rec z[N];
algorithm
z.x := x;
z.y := y;
end convert;
and call it via convert(size(c, 1), c.x, c.y).
Is there a simpler way?
I am trying to create a custom function for pearson's correlation coefficient with this code in matlab 2010
function [p] = customcorr(o)
x := a
y := b
x_mean := mean(a)
y_mean := mean(b)
x_std := std(a)
y_std := std(b)
n := length(o)
r := (1/(n-1))*((x-x_mean)*(y-y_mean))/(x_std*y_std)
end
But i get an error when trying to execute it
Error in ==> customcorr at 2
x := a
Anybody might know what the problem is? Thank you
First, check the correct MATLAB syntax: a "normal" assignment is done by =, not by :=.
Second, you use a and b but these are not defined as parameters of the function. Replace the function head by function p = customcorr(a,b).
Third, I am not really sure what o should be, I assume it can be replaced by length(a) or length(b).
The estimator for an unbiased correlation coefficient is given by
(from wikipedia)
Thus you need to sum all the (a-a_mean).*(b-b_mean) up with sum. Note that it is required to write .* to get the element-wise multiplication. That way you subtract the mean from each element of the vectors, then multiply the corresponding a's and b's and sum up the results of these multiplications.
Together this is
function p = customcorr(a,b)
a_mean = mean(a);
b_mean = mean(b);
a_std = std(a);
b_std = std(b);
n = length(a);
p = (1/(n-1)) * sum((a-a_mean).*(b-b_mean)) / (a_std*b_std);
end
What MATLAB does in their corr function (besides many other interesting things) is, they check the number of arguments (nargin variable) to see if a and b were supplied or not. You can do that by adding the following code to the function (at the beginning)
if nargin < 2
b = a;
end
This question already has answers here:
Generate a matrix containing all combinations of elements taken from n vectors
(4 answers)
Closed 8 years ago.
I am trying to make an index all possible molecules with 0-46 Hydrogen, 0-20 carbon, 0-13 oxygen, etc. I have 7 atoms in which I am interested: H, C, O, N, Cl, F, and S. I have written the following for loop to show what I am trying to achieve:
MassListIndex = []
%MassIndex = [h,c,o,n,cl,f,s]
for h = 0:46;
for c = 0:20;
for o = 0:13;
for n = 0:15;
for cl=0:5;
for f=0:5;
for s=0:5;
MassListIndex = [MassListIndex;[h,c,o,n,cl,f,s]];
end;
end;
end;
end;
end;
end;
end;
This strikes me as terribly inefficient; I don't want to wait around for 2 months for this to run. I have tried using the combinator.m script, but the problem is that there is only one input for the length of the set that is 'permutated' ie if I want to have up to 46 hydrogens, I need to also have 46 of each of the other 6 atoms. This is computationally...heavy (46^7 ~= 436 billion).
Is there any way to make this sort of computation more efficient? Or do I need to think more about shrinking my list by riding it of 'nonsense permutations' (As far as I know, the molecule H40C2 has never been observed!)
Thanks
The first problem is not that hard. At least not if you remember to preallocate!
I changed you the code into this:
mxidx = 47*21*14*16*6*6*6;
MassListIndex = zeros(mxidx,7);
idx = 1;
for h = 0:46;
for c = 0:20;
for o = 0:13;
for n = 0:15;
for cl=0:5;
for f=0:5;
for s=0:5;
MassListIndex(idx,:) = [h,c,o,n,cl,f,s];
idx = idx + 1;
end;
end;
end;
end;
end;
end;
end;
And it ran in less than a minute on my computer.
Usually Matlab will warn you if you forget to preallocate; and whenever you (like in this case) know in advance the size of you matrix, you should preallocate!
The other problem on the other hand, 47^7 = 506623120463 (more than 500 billions - it is 47^7 instead of 46^7 since the list 0:46 has 47 elements). So even if you only use one byte pr. row in you matrix (which you certainly don't) it will still take up more that a half terabyte! And the calculation times will likewise be humongous!
But really when would you ever need this list. The way you have constructed your list you can easily calculate an entry just by the index eg.:
function m = MassListIndex(a,b)
a = a - 1;
lst = zeros(1,7);
for i = 1:7
lst(8-i) = mod(a,47);
a = floor(a /47);
end
if nargin < 2
m = lst;
else
m = lst(b);
end
end
Edit:
If you want it to also calculate the mass, you may do something like:
function mass = getMassFromPermutationNumber(a)
a = a - 1;
lst = zeros(1,7);
for i = 1:7
lst(8-i) = mod(a,47);
a = floor(a /47);
end
mass = lst*[1.00794;12.011;15.9994;20.1797;35.4527;18.9984;32.066];
end
Source for masses: http://environmentalchemistry.com/yogi/periodic/mass.html
Disclaimer: I'm not that good at chemistry, so please apply reasonable amounts of skepticism!