Re: [Scilab-users] vector operation replacing for loop

2018-11-07 Thread kjubo



Hello,

precalculate the know values, I gain 40% faster execution time.
Hope helps a bit...

BR

clc, clear, mode(0)

n=1e6
Z=grand(1,n,'nor',0,1);
Z=Z(:);
r=0.9;

tic()
V1=Z;
for ii=2:n;
V1(ii)=r*V1(ii-1)+sqrt(1-r^2)*Z(ii);
end;
toc()

tic()
V2 = Z;
k1 = sqrt(1-r^2).*Z;
for ii=2:n;
V2(ii) = r*V2(ii-1) + k1(ii);
end;
toc()

isequal(V1,V2)




--
Sent from: 
http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


[Scilab-users] filter(): How to use the last zi argument <= Re: vector operation replacing for loop

2018-11-07 Thread Samuel Gougeon

Hello,

Is there anyone here that uses to use the last filter() optional argument?

In the example of the thread reminded below:

V = Z;
for i=2:n
   V(i) = a*V(i-1) + b*Z(i);
end

the input is Z, the output is V, a and b are fixed parameters.
The filter() description tells:


Syntax
--
 [y,zf] = filter(B, A, x [,zi])

Parameters
--
  B  : real vector : the coefficients of the filter numerator in 
decreasing power order, or

   a polynomial.
  A  : real vector : the coefficients of the filter denominator in 
decreasing power order,

   or a polynomial.
  x  : real row vector : the input signal
  zi : real row vector of length max(length(a),length(b))-1: the 
initial condition
   relative to a "direct form II transposed" state space 
representation. The default

   value is a vector filled with zeros.


So here i assume that zi is V(0) (so actually Z(1)). Then, how to get
[Z(1) V(2:n)]
as from the above loop? I have tried the following, without success:
y = filter(b, [1 -a], Z);
or
y = [Z(1) filter(b, [1 -a], Z(2:$), Z(1))];
or
y = filter(b, [1 -a], Z, Z(1)*(1-b)/a);

The zi option has no example, and unless filter() is bugged, the way zi 
works/is taken into account is unclear.


Any hint (before analyzing its hard code..)?

Regards
Samuel


Le 01/11/2018 à 15:16, Samuel Gougeon a écrit :

Le 01/11/2018 à 13:40, Samuel Gougeon a écrit :

Hello,

Le 01/11/2018 à 09:27, Heinz Nabielek a écrit :

Sorry, Scilab friends, I am still not fluid with vector operations.
Can someone rewrite the for loop for me into something much more efficient?

n=1000;
Z=grand(1,n,'nor',0,1);
r=0.9;
V=Z;
for i=2:n;
V(i)=r*V(i-1)+sqrt(1-r^2)*Z(i);
end;

The transformation generates an autocorrelated (here rho=0.9) normal 
distribution V from an uncorrelated normal distribution Z and eventually I will 
need it for very much larger n values


You may use filter(), with a feedback component (since V(i) depends 
on the previous state V(i-1) computed at the previous step).
However, as shown below, a quick trial shows an initial discrepancy 
between filter() result and yours with the explicit loop.
I don't know why. May be the setting for the initial condition should 
be carefully considered/tuned...


This is certainly the reason. For i=1, V(i-1) is unknown and must be 
somewhat provided. The last filter() option zi is not really 
documented (i have no time to go to the reference to get clear about 
how "the initial condition relative to a "direct form II transposed" 
state space representation." works. Trying to provide a value V(0) 
such that V(1)==Z(1) decreases the initial discrepancy by a factor 10. 
But it is still non zero.

y  =  filter(sqrt(1-r^2), [1  -r],  Z,   Z(1)*(1-sqrt(1-r^2))/r);



___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users



___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


Re: [Scilab-users] [EXTERNAL] Re: function and vectorization

2018-11-07 Thread Carrico, Paul
Thanks Stéphane

Works fine and fast (I'm speaking about my application :-) )

Paul

EXPORT CONTROL :
Cet email ne contient pas de données techniques
This email does not contain technical data

De : users [mailto:users-boun...@lists.scilab.org] De la part de Stéphane 
Mottelet
Envoyé : mercredi 7 novembre 2018 13:40
À : users@lists.scilab.org
Objet : [EXTERNAL] Re: [Scilab-users] function and vectorization

Hello,

Try:

function [Scar_P]=Scalar_product(C, N, M) // Scalar product



Scar_P = (M(:,1) - C(:,1)).*(N(:,1) - C(:,1)) + (M(:,2) - C(:,2)).*(N(:,2) 
- C(:,2));



//printf("C = (%g,%g)\n",C(1),C(2)); printf("N = (%g,%g)\n",N(1),N(2)); 
printf("M = (%g,%g)\n",M(1),M(2));



endfunction



with (do not transpose N(i,1:2))
i = 1 : n;
Scal(i) = Scalar_product(C(i,:),N(i,1:2),M(i,:))

S.

Le 07/11/2018 à 12:35, Carrico, Paul a écrit :

#
mode(0)
clear

function [Scar_P]=Scalar_product(C, N, M) // Scalar product
Scar_P = (M(1) - C(1))*(N(1) - C(1)) + (M(2) - C(2))*(N(2) - C(2));
printf("C = (%g,%g)\n",C(1),C(2)); printf("N = (%g,%g)\n",N(1),N(2)); 
printf("M = (%g,%g)\n",M(1),M(2));
endfunction

n = 10;

C = rand(n,2);
M = rand(n,2);
N = rand(n,5);

Scal = zeros(n); Scal2 = Scal

printf(" \n");
i = 1 : n;
Scal(i) = Scalar_product(C(i,:),N(i,1:2)',M(i,:))

printf("\n \n");

for i = 1 : n
Scal2(i) = Scalar_product(C(i,:),N(i,1:2)',M(i,:));
end



--

Stéphane Mottelet

Ingénieur de recherche

EA 4297 Transformations Intégrées de la Matière Renouvelable

Département Génie des Procédés Industriels

Sorbonne Universités - Université de Technologie de Compiègne

CS 60319, 60203 Compiègne cedex

Tel : +33(0)344234688

http://www.utc.fr/~mottelet
___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


Re: [Scilab-users] function and vectorization

2018-11-07 Thread CRETE Denis
Hello,

If your matrices are not too big,

diag((M-C)*(N(:,1:2)-C)')
may do the job: it gives the same result as Stéphane's solution
HTH
Denis


[@@ THALES GROUP INTERNAL @@]

Unité Mixte de Physique CNRS / THALES
1 Avenue Augustin Fresnel
91767 Palaiseau CEDEx - France
Tel : +33 (0)1 69 41 58 52 Fax : +33 (0)1 69 41 58 78
e-mail :
 denis.cr...@thalesgroup.com 

http://www.trt.thalesgroup.com/ump-cnrs-thales
http://www.research.thalesgroup.com

De : users [mailto:users-boun...@lists.scilab.org] De la part de Stéphane 
Mottelet
Envoyé : mercredi 7 novembre 2018 13:40
À : users@lists.scilab.org
Objet : Re: [Scilab-users] function and vectorization

Hello,

Try:

function [Scar_P]=Scalar_product(C, N, M) // Scalar product



Scar_P = (M(:,1) - C(:,1)).*(N(:,1) - C(:,1)) + (M(:,2) - C(:,2)).*(N(:,2) 
- C(:,2));



//printf("C = (%g,%g)\n",C(1),C(2)); printf("N = (%g,%g)\n",N(1),N(2)); 
printf("M = (%g,%g)\n",M(1),M(2));



endfunction



with (do not transpose N(i,1:2))
i = 1 : n;
Scal(i) = Scalar_product(C(i,:),N(i,1:2),M(i,:))

S.

Le 07/11/2018 à 12:35, Carrico, Paul a écrit :

#
mode(0)
clear

function [Scar_P]=Scalar_product(C, N, M) // Scalar product
Scar_P = (M(1) - C(1))*(N(1) - C(1)) + (M(2) - C(2))*(N(2) - C(2));
printf("C = (%g,%g)\n",C(1),C(2)); printf("N = (%g,%g)\n",N(1),N(2)); 
printf("M = (%g,%g)\n",M(1),M(2));
endfunction

n = 10;

C = rand(n,2);
M = rand(n,2);
N = rand(n,5);

Scal = zeros(n); Scal2 = Scal

printf(" \n");
i = 1 : n;
Scal(i) = Scalar_product(C(i,:),N(i,1:2)',M(i,:))

printf("\n \n");

for i = 1 : n
Scal2(i) = Scalar_product(C(i,:),N(i,1:2)',M(i,:));
end



--

Stéphane Mottelet

Ingénieur de recherche

EA 4297 Transformations Intégrées de la Matière Renouvelable

Département Génie des Procédés Industriels

Sorbonne Universités - Université de Technologie de Compiègne

CS 60319, 60203 Compiègne cedex

Tel : +33(0)344234688

http://www.utc.fr/~mottelet
___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


Re: [Scilab-users] function and vectorization

2018-11-07 Thread Stéphane Mottelet

Hello,

Try:

function  [Scar_P]=Scalar_product(C, N, M)  // Scalar product

Scar_P  =  (M(:,1)  -  C(:,1)).*(N(:,1)  -  C(:,1))  +  (M(:,2)  -  
C(:,2)).*(N(:,2)  -  C(:,2));

//printf("C = (%g,%g)\n",C(1),C(2)); printf("N = (%g,%g)\n",N(1),N(2)); 
printf("M = (%g,%g)\n",M(1),M(2));


endfunction with (do not transpose N(i,1:2))

i= 1 : n;

Scal(i)= _Scalar_product_(C(i,:),N(i,1:2),M(i,:))


S.

Le 07/11/2018 à 12:35, Carrico, Paul a écrit :


#

mode(0)

clear

function[*Scar_P*]=_Scalar_product_(*C*, *N*, *M*) /// Scalar product/

*Scar_P* = (*M*(1) - *C*(1))*(*N*(1) - *C*(1)) + (*M*(2) - 
*C*(2))*(*N*(2) - *C*(2));


printf("C = (%g,%g)\n",*C*(1),*C*(2)); printf("N = 
(%g,%g)\n",*N*(1),*N*(2)); printf("M = (%g,%g)\n",*M*(1),*M*(2));


endfunction

n= 10;

C= rand(n,2);

M= rand(n,2);

N= rand(n,5);

Scal= zeros(n); Scal2 = Scal

printf(" \n");

i= 1 : n;

Scal(i)= _Scalar_product_(C(i,:),N(i,1:2)',M(i,:))

printf("\n \n");

fori = 1 : n

Scal2(i) = _Scalar_product_(C(i,:),N(i,1:2)',M(i,:));

end



--
Stéphane Mottelet
Ingénieur de recherche
EA 4297 Transformations Intégrées de la Matière Renouvelable
Département Génie des Procédés Industriels
Sorbonne Universités - Université de Technologie de Compiègne
CS 60319, 60203 Compiègne cedex
Tel : +33(0)344234688
http://www.utc.fr/~mottelet

___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


[Scilab-users] function and vectorization

2018-11-07 Thread Carrico, Paul
Dear All

Most of the time I've no issue in mixing functions and vectorization, but here 
I don't know why it does not work - one can see that using a vector I, the loop 
is called only ounce and I do not understand why?

I've spent hours in such case  showing I'm not fully at ease with it :-) :-)

Any explanation?

Thanks for your support

Paul

#
mode(0)
clear

function [Scar_P]=Scalar_product(C, N, M) // Scalar product
Scar_P = (M(1) - C(1))*(N(1) - C(1)) + (M(2) - C(2))*(N(2) - C(2));
printf("C = (%g,%g)\n",C(1),C(2)); printf("N = (%g,%g)\n",N(1),N(2)); 
printf("M = (%g,%g)\n",M(1),M(2));
endfunction

n = 10;

C = rand(n,2);
M = rand(n,2);
N = rand(n,5);

Scal = zeros(n); Scal2 = Scal

printf(" \n");
i = 1 : n;
Scal(i) = Scalar_product(C(i,:),N(i,1:2)',M(i,:))

printf("\n \n");

for i = 1 : n
Scal2(i) = Scalar_product(C(i,:),N(i,1:2)',M(i,:));
end

EXPORT CONTROL :
Cet email ne contient pas de données techniques
This email does not contain technical data

___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users