2

I have used expl3 to sum the terms of a given array, however as you can see the procedure is quite cumbersome.

Is there a more elegant way to do the same thing?

\documentclass{article}
\usepackage{xparse}
\usepackage{siunitx} 
\ExplSyntaxOn
\fparray_new:Nn \g_nome {6}
\fparray_gset:Nnn \g_nome {1}{0.907}
\fparray_gset:Nnn \g_nome {2}{0.875}
\fparray_gset:Nnn \g_nome {3}{0.845}
\fparray_gset:Nnn \g_nome {4}{0.817}
\fparray_gset:Nnn \g_nome {5}{0.701}
\fparray_gset:Nnn \g_nome {6}{0.613}
%
\fp_new:N \somma
%
\fp_zero:N \somma
%
\fp_set:Nn \somma {\somma + \fparray_item:Nn \g_nome {1}}
\fp_set:Nn \somma {\somma + \fparray_item:Nn \g_nome {2}}
\fp_set:Nn \somma {\somma + \fparray_item:Nn \g_nome {3}}
\fp_set:Nn \somma {\somma + \fparray_item:Nn \g_nome {4}}
\fp_set:Nn \somma {\somma + \fparray_item:Nn \g_nome {5}}
\fp_set:Nn \somma {\somma + \fparray_item:Nn \g_nome {6}}

\NewDocumentCommand{\calcnumd}{o m}
  {\IfValueTF{#1}
     {\num[round-mode = figures, round-precision = #1, round-integer-to-decimal]{\fp_to_decimal:n{#2}}}
     {\num{\fp_to_decimal:n{#2}}}
  }

\ExplSyntaxOff
\begin{document}
The sum of the elements of array is:

\[ S = \calcnumd{\somma}  \]

\end{document}

2 Answers 2

2

You can sum the array using \int_step_function:nN

\documentclass{article}
\usepackage{xparse}
\usepackage{siunitx} 

\ExplSyntaxOn

\fparray_new:Nn \g_albystalks_main_fparray {6}
\fparray_gset:Nnn \g_albystalks_main_fparray {1}{0.907}
\fparray_gset:Nnn \g_albystalks_main_fparray {2}{0.875}
\fparray_gset:Nnn \g_albystalks_main_fparray {3}{0.845}
\fparray_gset:Nnn \g_albystalks_main_fparray {4}{0.817}
\fparray_gset:Nnn \g_albystalks_main_fparray {5}{0.701}
\fparray_gset:Nnn \g_albystalks_main_fparray {6}{0.613}

\cs_new:Nn \albystalks_sum_fparray:
 {
  \fp_eval:n
   {
    \int_step_function:nN { \fparray_count:N \g_albystalks_main_fparray }
      \__albystalks_fparray_item:n
   }
 }

\cs_new:Nn \__albystalks_fparray_item:n
 {
  + ( \fparray_item:Nn \g_albystalks_main_fparray { #1 } )
 }

\NewDocumentCommand{\calcnumd}{o}
 {
  \IfValueTF{#1}
   {
    \num
     [
      round-mode = figures,
      round-precision = #1,
      round-integer-to-decimal
     ]
     {
      \albystalks_sum_fparray:
     }
   }
   {
    \num { \albystalks_sum_fparray: } }
 }

\ExplSyntaxOff

\begin{document}

The sum of the elements of array is $S = \calcnumd$

Rounding to two digit precision, we have $S=\calcnumd[2]$

\end{document}

enter image description here

1

You can use a loop to sum the items. I also changed the way the array is constructed, just to provide an alternative idea.

\documentclass{article}
\usepackage{xparse}
\usepackage{siunitx} 


\ExplSyntaxOn

\clist_set:Nn \l_tmpa_clist {0.907, 0.875, 0.845, 0.817, 0.701, 0.613}

\exp_args:NNx \fparray_new:Nn \g_nome {\clist_count:N \l_tmpa_clist}

\int_set:Nn \l_tmpa_int {1}
\int_do_until:nNnn {\l_tmpa_int} > {\fparray_count:N \g_nome} {
    \fparray_gset:Nnn \g_nome {\l_tmpa_int}{\clist_item:Nn \l_tmpa_clist {\l_tmpa_int}}
    \int_incr:N \l_tmpa_int
}

\fp_new:N \somma
\fp_zero:N \somma

\int_set:Nn \l_tmpa_int {1}
\int_do_until:nNnn {\l_tmpa_int} > {\fparray_count:N \g_nome} {
    \fp_add:Nn \somma { \fparray_item:Nn \g_nome {\l_tmpa_int} }
    \int_incr:N \l_tmpa_int
}

\NewDocumentCommand{\calcnumd}{o m}
  {\IfValueTF{#1}
     {\num[round-mode = figures, round-precision = #1, round-integer-to-decimal]{\fp_to_decimal:n{#2}}}
     {\num{\fp_to_decimal:n{#2}}}
  }

\ExplSyntaxOff


\begin{document}
The sum of the elements of array is:

\[ S = \calcnumd{\somma}  \]

\end{document}
1
  • This solution will help me in another problem. Aug 3, 2020 at 15:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .