Stéphane,

Thanks, this is great, the use of eye is the way to go to overcome the problem of an earlier suggestion which discarded, for instance, A + 2 for not being what Horner expects; but A + 2*eye(A) is!

Thanks also for making me aware of the simplified syntax 2*eye() which adapts its size to the matrix it is added to, and is also computationally more efficient, important especially for very large matrices.

Best regards,

Federico Miyara


On 24/09/2019 05:34, Stéphane Mottelet wrote:

Why just not writing Horner's algorithm directly ?

functionout=horner_mat(p, X)a=coeff(p)out=zeros(X);fork=degree(p):-1:0out=out*X+a(k+1)*eye()endendfunction--> a=[1 2;3 4]a = 1. 2.3. 4.--> horner_mat(1+%s-%s^2,a)ans =-5. -8. -12. -17.--> eye() + a - a^2ans =-5. -8. -12. -17.S.
Le 24/09/2019 à 10:31, Dang Ngoc Chan, Christophe a écrit :
Hi,

I've got some time this morning (-: so this one seems to work and should be a 
bit more efficient:

// **********

P = %s^2 + 2*%s + 3

C = coeff(P)

n = size(C, "c")

if n > 1 then

     stringP = "X1 = x; "

     for i = 3:n

         stringP = stringP+"X"+string(i-1)+" = X"+string(i-2)+"*x; "

     end

else

     stringP = ""

end

stringP = stringP+"Y = "+string(C(1))

for i = 2:n

     stringP = stringP+" + "+string(C(i))+"*X"+string(i-1)

end

deff("[Y] = fctP(x)", stringP)

// **********

Regards

--
Christophe Dang Ngoc Chan
Mechanical calculation engineer


General
This e-mail may contain confidential and/or privileged information. If you are 
not the intended recipient (or have received this e-mail in error), please 
notify the sender immediately and destroy this e-mail. Any unauthorized 
copying, disclosure or distribution of the material in this e-mail is strictly 
forbidden.
_______________________________________________
users mailing list
users@lists.scilab.org
https://antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/lists.scilab.org/mailman/listinfo/users
--
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



---
El software de antivirus Avast ha analizado este correo electrónico en busca de 
virus.
https://www.avast.com/antivirus
_______________________________________________
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users

Reply via email to