Using the VBA ParamArray keyword

The recently posted SolvePoly function makes use of the VBA ParamArray keyword, which allows an arbitrarily long list of arguments to be passed to another function.  I have not made great use of this in the past, but since the structure is similar to a Python list of lists it forms a convenient way of transferring data to and from Python routines, as well as being useful in a pure VBA context.

The full code of the PolySolve function is shown below:

Function SolvePoly(ParamArray CoeffA() As Variant)
Dim i As Long, PArray As Variant, Num_Coeff As Long

    Num_Coeff = UBound(CoeffA) + 1
    ReDim PArray(1 To Num_Coeff, 1 To 1)
    For i = 0 To Num_Coeff - 1
        PArray(i + 1, 1) = CoeffA(i).Value2
    Next i

    Select Case Num_Coeff
    Case Is < 3:
        SolvePoly = "Num_Coeff must be >= 3"
    Case Is < 4:
        SolvePoly = Quadratic(PArray)
    Case 4:
        SolvePoly = CubicC(PArray)
    Case 5:
        SolvePoly = Quartic(PArray)
    Case Else:
        SolvePoly = RPolyJT(PArray)
    End Select
    SolvePoly = WorksheetFunction.Transpose(SolvePoly)
End Function

The rules for using ParamArray are:

  • Any non-optional arguments must be declared first.
  • The argument passed by ParamArray must be declared as a variant array, including the “()”.
  • ParamArray cannot be used in conjunction with ByVal, ByRef or Optional.
  • Each argument in the ParamArray array may be any data type, including variant arrays, ranges or objects.
  • The argument passed by ParamArray is a 1D base 0 array
  • Each argument is optional, but if optional arguments are expected they must be checked using the IsMissing function.  In the case of the SolvePoly function an empty argument in the list (e.g. =solvepoly($B$12,$B$13,,C12)) will cause an error at the line:  PArray(i + 1, 1) = CoeffA(i).Value2
  • When calling the function, either from the worksheet or from another VBA function, each argument is listed separately.  They are combined into a variant array automatically.

In the case of the SolvePoly function the CoeffA argument was required to pass an arbitrary number of double values, which were then converted into a 2D array, as expected by the subsequently called functions.  A Select Case statement was then used to call one of 4 functions, depending on the length of the PArray array.  Note that the final RPolyJT function will accept an array of any length, so PolySolve will handle any number of input arguments greater than 2.

Posted in Excel, Maths, Newton, UDFs, VBA | Leave a comment

Is Hinchliffe’s Rule True? …

… is the title of a paper by Boris Peon. Here is the abstract:

Hinchliffe has asserted that whenever the title of a paper is a question with a yes/no answer, the answer is always no. This paper demonstrates that Hinchliffe’s assertion is false, but only if it is true.

From New Scientist, 16 August 2014

Download the full paper

 

Posted in Newton | Tagged | 2 Comments

Long Lost, a WW1 short film …

… and crowd-funded production, directed by my daughter:

The story of the project

At the start of 2014, sparked by the funding opportunity Raw Nerve (A Metroscreen and Screen Australia initiative) Kerinne Jenkins (director), Harry Windsor (writer) and Nic Douglas (producer) conceived of the short film “ Long Lost”.

After applying with a video pitch, our project was chosen as one of three successful candidates. We were then invited to participate in a three-month development process with the other Breaks filmmakers and mentors Lachlan Philpott, Karel Segers and Jonathan Wald.

Now that the script has been developed, we’ve been given a start in realizing this project, with an initial amount of funding and it’s up to us to raise the rest to bring this story to life.

Visit the link below, have a look at the video with more on the background to the project, and contribute to allow it to proceed:

Long Lost – crowd funding site

Posted in Bach, Films | Tagged , , | Leave a comment

Numerical Integration; Tanh-Sinh Quadrature v. 4.2

The latest version (4.2) of the numerical integration spreadsheet by Graeme Dennes is now available for download from Tanh_Sinh Quadrature.

For more details of the background see: Faster Integration with the Tanh-Sinh Method and subsequent posts on this subject.

In addition to the code and examples on the use of Tanh-Sinh quadrature and a number of other techniques, the download file contains examples and thoroughly documented code for a range of other spreadsheet and VBA functions, including:

  • Dynamic linking of graphs to function results, without writing the results to the spreadsheet
  • Evaluating functions entered as text on the spreadsheet
  • Timing of function execution time with a high precision timer
  • Accurate determination of processor constants, such as maximum an minimum floating point values.
  • VBA code for the complete range of standard trigonometry functions
  • Examples of incorporating the quadrature functions in other routines.

The revisions included in Ver. 4.2 are listed below.  In the next post in this series I will look in more detail at how these functions can be incorporated in other spreadsheets.

Version 4.2 Release Notes

  1. Following a private suggestion, all quadrature programs now provide the number of function evaluations as a more useful performance metric.
  2. The Romberg program runs in 30 percent of the time taken by the previous (V4.1) release, and accuracy averages only one digit less than Tanh-Sinh. The Romberg algorithm implemented herein by the author may be the fastest and most accurate to date.
  3. The function plotter is located on a separate worksheet for convenience.
  4. Finite interval test functions total 400, which may be the largest set of diverse test integrals available (with answers) at no cost.

Graeme Dennes

 

Posted in Excel, Maths, Newton, Numerical integration, UDFs, VBA | Tagged , , , , , , , , | 2 Comments

Solving polynomials – update

The polynomial spreadsheet (details here) provides functions to solve polynomial equations of any order; using an “exact” method for up to quartic, and an iterative procedure for higher orders.  The input for the functions requires the equation coefficients to be in a continuous column or row range.  This is convenient for many cases, but there are times when it is necessary to enter each coefficient separately.

I have now added a new function to the spreadsheet, which has the coefficients entered separately, and calls the appropriate solver function, depending on the number of coefficients.  The new function, including full open source code, may be downloaded from Polynomial.zip.The screenshot below shows an example of usage of the new function (included in the download file):

Polynomial1In this example three coefficients of a cubic equation are constant, but the fourth varies. The solution to the equation can be set up easily as follows:

  • Enter the values for coefficients a to c in any convenient range.
  • Enter the values for coefficient d in a column
  • In the cell adjacent to the top of the d column, enter the SolvePoly function:
    =solvepoly($B$12,$B$13,$B$14,C12).  Note the $ signs, making the first three addresses absolute (i.e. they will not change when the function is copied).
  • To display the three solutions and the number of real roots enter the function as an array function: select the function cell and the adjacent three cells; press F2; press ctrl-shift-enter.
  • These four cells may now be copied to the clipboard and pasted over as many rows as required.  Note that the first three coefficients always refer to the fixed range, but the d coefficient changes as it is copied down.
Posted in Arrays, Excel, Maths, UDFs, VBA | Tagged , , , , , | 1 Comment