Discussion:
[Scilab-users] function and vectorization
Carrico, Paul
2018-11-07 11:35:34 UTC
Permalink
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
Stéphane Mottelet
2018-11-07 12:39:43 UTC
Permalink
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.
Post by Carrico, 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");
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
CRETE Denis
2018-11-07 12:54:00 UTC
Permalink
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 :
***@thalesgroup.com<mailto:***@thalesgroup.com> <mailto:%***@thalesgroup.com>
http://www.trt.thalesgroup.com/ump-cnrs-thales
http://www.research.thalesgroup.com

De : users [mailto:users-***@lists.scilab.org] De la part de Stéphane Mottelet
Envoyé : mercredi 7 novembre 2018 13:40
À : ***@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
Carrico, Paul
2018-11-07 13:28:37 UTC
Permalink
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-***@lists.scilab.org] De la part de Stéphane Mottelet
Envoyé : mercredi 7 novembre 2018 13:40
À : ***@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<https://urldefense.proofpoint.com/v2/url?u=http-3A__www.utc.fr_-7Emottelet&d=DwMG-g&c=0hKVUfnuoBozYN8UvxPA-w&r=4TCz--8bXfJhZZvIxJAemAJyz7Vfx78XvgYu3LN7eLo&m=4nq72IuY_O4BD4bc-E25iQkCiD8-ZEBjCl6xzxjHbWw&s=9-ZrmcA1S1rslVYW5SkH4zF_TANcPxDeWItXi95Gqls&e=>
Loading...