User Tools

Site Tools


finding_sum_mod_partial_array

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
finding_sum_mod_partial_array [2023/01/06 14:22] – created richardrussellfinding_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 illustrate their use, this code calculates the Standard Deviation of elements **1** to **8** inclusive of an array:+To find the sum or modulus of a **subset** of the elements you can use the user-defined functions below.  To illustrate their use, this code calculates the Standard Deviation of elements **1** to **8** inclusive of an array:
  
 <code bb4w> <code bb4w>
Line 13: Line 13:
       array() -= mean       array() -= mean
       PRINT FNmod(array(),1,8) / SQR(8)       PRINT FNmod(array(),1,8) / SQR(8)
 +      array() += mean
 </code> </code>
  
-This prints 2.29128785, being the Standard Deviation of the values 2,3,4,5,6,7,8 and 9.+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.  Please be aware that the array is temporarily modified, so **do not** access it in a timer interrupt or other asynchronous event handler:
  
 <code bb4w> <code bb4w>
Line 23: Line 24:
       IF DIM(a())<>1 ERROR 0, "Array must be one-dimensional"       IF DIM(a())<>1 ERROR 0, "Array must be one-dimensional"
       IF I% < 0 OR N% < 1 OR I% + N% - 1 > DIM(a(),1) ERROR 0, "Invalid array limits"       IF I% < 0 OR N% < 1 OR I% + N% - 1 > DIM(a(),1) ERROR 0, "Invalid array limits"
-      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())<>1 ERROR 0, "Array must be one-dimensional"       IF DIM(a())<>1 ERROR 0, "Array must be one-dimensional"
       IF I% < 0 OR N% < 1 OR I% + N% - 1 > DIM(a(),1) ERROR 0, "Invalid array limits"       IF I% < 0 OR N% < 1 OR I% + N% - 1 > DIM(a(),1) ERROR 0, "Invalid array limits"
-      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())
 </code> </code>
 +
 +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)