sum of a list or vector that can sometimes have a single value

sum of a list or vector that can sometimes have a single value - Mensajes

#1 Publicado: 18/4/2022 9:20:55
Laurent Fournier

Laurent Fournier

9 me gusta en 66 mensajes.

Grupo: User

Hello,
I have tried this function in order to be able to add together all the elements of a list (or vector) of variable length, including having one single element, but I don't know how to make it work.

suml(a):=if IsDefined(length(a))
sum(a)
else
a

It says "syntax is incorrect"...

Any suggestion?

If the boolean testing is "false" then is the "true" option evaluated or not? (I guess it shouldn't...)

Thanks in advance
Laurent
#2 Publicado: 18/4/2022 9:50:44
Davide Carpi

Davide Carpi

1.443 me gusta en 2.936 mensajes.

Grupo: Moderator

Screenshot_20220418_144951.jpg
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#3 Publicado: 18/4/2022 9:51:14
Laurent Fournier

Laurent Fournier

9 me gusta en 66 mensajes.

Grupo: User

Hello I found a solution:

suml(a):=line(
a:=stack(a,0)
sum(a)
)

Sorry to have bothered this forum with such a basic question !



Wrote

Hello,
I have tried this function in order to be able to add together all the elements of a list (or vector) of variable length, including having one single element, but I don't know how to make it work.

suml(a):=if IsDefined(length(a))
sum(a)
else
a

It says "syntax is incorrect"...

Any suggestion?

If the boolean testing is "false" then is the "true" option evaluated or not? (I guess it shouldn't...)

Thanks in advance
Laurent



#4 Publicado: 18/4/2022 9:52:20
Laurent Fournier

Laurent Fournier

9 me gusta en 66 mensajes.

Grupo: User

Wrote

Screenshot_20220418_144951.jpg



Great !
I learned something new!
Thanks a lot.
#5 Publicado: 18/4/2022 10:22:36
Davide Carpi

Davide Carpi

1.443 me gusta en 2.936 mensajes.

Grupo: Moderator

You're welcome

Note that using the stack approach you don't need the zero

Screenshot_20220418_151248.jpg
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#6 Publicado: 18/4/2022 11:07:54
Laurent Fournier

Laurent Fournier

9 me gusta en 66 mensajes.

Grupo: User

Wrote

You're welcome

Note that using the stack approach you don't need the zero

Screenshot_20220418_151248.jpg



Ha! :d :d That's puzzling! You can "stack" nothing on something!

Anyway, around this remarquable Smath Studio software is a no less remarquable community!
  • Nuevos mensajes
  • Sin mensajes nuevos