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;
Related
I am trying to create a bin in my coverage group to sample values that are multiple of n (where n is a constant integer in my case 15). So far, I have came
up with the following code:
class rx_port;
int unsigned rx_rates[];
...
covergroup rx_cov with function sample (int unsigned rate);
coverpoint rate{
bins no_rate = {0};
bins mul_of_15 = {SOME_PRE_DEFINED_PATTERN};
}
endgroup;
....
endclass
Where SOME_PRE_DEFINED_PATTERN is an array of int from 0 to a system macro with the step of 15. I am not sure if this is the correct/best way of generating this bin. Any better suggestion?
How about writing some helper functions:
module FIFTEEN;
class rx_port;
typedef enum {IS_ZERO, IS_DIVISIBLE_BY_15, IS_NOT_DIVISIBLE_BY_15} rate_type;
function new;
rx_cov=new;
endfunction
local function rate_type covergroup_helper(input int unsigned i);
if (i==0) return IS_ZERO;
if (i%15==0) return IS_DIVISIBLE_BY_15;
return IS_NOT_DIVISIBLE_BY_15;
endfunction
function sample (input int unsigned i);
rx_cov.sample(covergroup_helper(i));
endfunction
covergroup rx_cov with function sample (rate_type rate);
coverpoint rate;
endgroup;
endclass
rx_port R = new;
initial
begin
void'(R.sample(0));
void'(R.sample(30));
void'(R.sample(31));
$display("coverage R.rx_cov.get_coverage= %f", R.rx_cov.get_coverage);
end
endmodule
https://www.edaplayground.com/x/65v7
Here I've written a function that determines whether its input is divisible by 15 or not and another function which calls that to do the sampling. You could combine those functions together, but I like the division of labour in my example.
It turns out that there is a better way:
module FIFTEEN;
class rx_port;
function new;
rx_cov=new;
endfunction
function sample (input int unsigned i);
rx_cov.sample(i);
endfunction
covergroup rx_cov with function sample (int unsigned rate);
coverpoint rate {
bins IS_ZERO = {0};
bins IS_DIVISIBLE_BY_15 = {[1:$]} with ((item % 15)==0);
bins IS_NOT_DIVISIBLE_BY_15 = {[1:$]} with ((item % 15)!=0);
}
endgroup;
endclass
rx_port R = new;
initial
begin
void'(R.sample(0));
void'(R.sample(30));
void'(R.sample(31));
$display("coverage R.rx_cov.get_coverage= %f", R.rx_cov.get_coverage);
end
endmodule
https://www.edaplayground.com/x/3T5v
You can use with to specify bins. So
bins IS_DIVISIBLE_BY_15 = {[1:$]} with ((item % 15)==0);
gives you a bin that is hit whenever the value is divisible by 15 (but not 0) and
bins IS_NOT_DIVISIBLE_BY_15 = {[1:$]} with ((item % 15)!=0);
gives you a bin that is hit whenever the value is not divisible by 15.
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?
Hello everybody I try to find Reduced row echelon form for binary matrix in matlab. Does mathlab contain any functions to do that?
Standart rref computes it only for real numbers.
Thanks
[m,n]=size(a);
b=a;
c=0;
for k=1:min(m,n)
c=c+1;
i=find(b(:,k));
i(i strictly less c)=[ ];
if (~isempty(i))
imax=min(i);
b([c imax],:)=b([imax c],:);
ck=b(c,:);
p=find(b(:,k));
p(p==c)=[];
if ~isempty(p)
v=b(p,:)+repmat(ck,length(p),1);
b(p,:)=mod(v,2);
end;
else
c=c-1;
end;
end;
I have 4 buttons on an FPGA dev board so I wrote
function [HEX0] = Bar(KEY)
n = uint8(sum(KEY, 'native'));
...
Unfortunately, HDL Coder turned it into the following chunk of VHDL:
y := '0';
FOR k IN 0 TO 3 LOOP
y := y OR KEY(k);
END LOOP;
y_0 := '0' & '0' & '0' & '0' & '0' & '0' & '0' & y;
Which I just don't get. Can you help me figure out what's going on here?
To understand this, you have to understand the matlab sum with logical inputs and native option. The sum of logicals is a logical. Thus sum could be replaced with an or
sum([true,true],'native')
And this is exactly what your Coder puts out. The for-Loop implements the sum (sum(KEY, 'native')), where the coder recognizes that it could be implemented using a OR.
Finally, conversion from logical to uint8 is done padding 7 zero bits.
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 ;