finding_sum_mod_partial_array
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| finding_sum_mod_partial_array [2023/01/06 14:22] – created richardrussell | finding_sum_mod_partial_array [2024/01/05 00:21] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| The **SUM()** and **MOD()** functions return the **sum** and **modulus** (square-root of the sum of the squares) respectively of a numeric array, and they are much faster than performing the calculations yourself in a loop. However you have no control over which elements are operated on: it is always the entire array (remember that in BBC BASIC array indices are zero-based). | The **SUM()** and **MOD()** functions return the **sum** and **modulus** (square-root of the sum of the squares) respectively of a numeric array, and they are much faster than performing the calculations yourself in a loop. However you have no control over which elements are operated on: it is always the entire array (remember that in BBC BASIC array indices are zero-based). | ||
| - | To find the sum or modulus of a subset of the elements you can use the user-defined functions below. | + | To find the sum or modulus of a **subset** of the elements you can use the user-defined functions below. |
| <code bb4w> | <code bb4w> | ||
| Line 13: | Line 13: | ||
| array() -= mean | array() -= mean | ||
| PRINT FNmod(array(), | PRINT FNmod(array(), | ||
| + | array() += mean | ||
| </ | </ | ||
| - | This prints 2.29128785, being the Standard Deviation of the values 2, | + | This prints 2.29128785, being the Standard Deviation of the values 2, 3, 4, 5, 6, 7, 8 and 9. |
| - | Here are the functions: | + | Here are the functions; the parameters are the array, the index of the first element to include, and the total number of elements over which to perform the operation. |
| <code bb4w> | <code bb4w> | ||
| Line 23: | Line 24: | ||
| IF DIM(a())<> | IF DIM(a())<> | ||
| IF I% < 0 OR N% < 1 OR I% + N% - 1 > DIM(a(),1) ERROR 0, " | IF I% < 0 OR N% < 1 OR I% + N% - 1 > DIM(a(),1) ERROR 0, " | ||
| - | LOCAL p%% : p%% = ^a(I%) - 5 : PTR(a()) = p%% | + | LOCAL p%% : p%% = ^a(I%) - 5 |
| - | LOCAL ?p%%, p%%!1 : ?p%% = 1 : p%%!1 = N% | + | LOCAL ?p%%, p%%!1 : ?p%% = 1 : p%%!1 = N% : PTR(a()) = p%% |
| = SUM(a()) | = SUM(a()) | ||
| Line 30: | Line 31: | ||
| IF DIM(a())<> | IF DIM(a())<> | ||
| IF I% < 0 OR N% < 1 OR I% + N% - 1 > DIM(a(),1) ERROR 0, " | IF I% < 0 OR N% < 1 OR I% + N% - 1 > DIM(a(),1) ERROR 0, " | ||
| - | LOCAL p%% : p%% = ^a(I%) - 5 : PTR(a()) = p%% | + | LOCAL p%% : p%% = ^a(I%) - 5 |
| - | LOCAL ?p%%, p%%!1 : ?p%% = 1 : p%%!1 = N% | + | LOCAL ?p%%, p%%!1 : ?p%% = 1 : p%%!1 = N% : PTR(a()) = p%% |
| = MOD(a()) | = MOD(a()) | ||
| </ | </ | ||
| + | |||
| + | In a speed-critical application you may choose to omit the error-checking statements if you are sure that the parameters are valid. | ||
finding_sum_mod_partial_array.1673014947.txt.gz · Last modified: 2024/01/05 00:16 (external edit)