Scipy Functions with Excel and pyxll 7 – Linear Algebra

This post looks at linking Excel to the Scipy linear algebra functions using pyxll, and also using the PyPardiso solver. Previous posts on this topic include:

The pyLinAlgfuncs3 spreadsheet, with associated Python code in pyLinalgfuncs3.py and pyScipy3.py, are included in the download file:

py_SciPy.zip

Details of the required pyxll package (including download, free trial, and full documentation) can be found at: pyxll

For those installing a new copy of pyxll, a 10% discount on the first year’s fees is available using the coupon code “NEWTONEXCELBACH10”.

The pyLinAlgfuncs3 spreadsheet includes links to many different solver functions and associated utility functions, but for most purposes the py_Solve (for dense matrices) or py_SpSolve (for sparse matrices) will be most efficient. Note that the py_SpSolve function calls the fast PyPardiso sparse solver if it is installed, or the Scipy spsolve function if not.

Links to the Scipy on-line help are available for most of the functions by opening the function dialog box, and clicking on “Help on this function” in the bottom left corner:

This links to the Scipy help for the associated Scipy fumction:

Input of the matrix to be solved may be in alternative formats:

  • A complete square matrix on the spreadsheet.
  • A linked list in COO format on the spreadsheet
  • A pyxll cache object. Note that this option allows links to Python generated arrays that would be too big to transfer to a spreadsheet, and will also be much faster for very large matrices.

The SpSolveit sheets has functions linking to 10 alternative sparse iterative solvers. 

See Speed of Scipy Linear Algebra Solvers for more information on the relative speed of the solvers for typical structural engineering applications.

The LuSolve sheet has functions for factorisation and solving using LU factorisation.

ChoSolve has Cholesky and banded solvers:

Python_LU has functions demonstrating background to the LU solver approach. See LU decomposition with python and scipy for more details.

Finally the Misc sheet has examples of various matrix related functions:

Posted in Arrays, Excel, Finite Element Analysis, Frame Analysis, Link to Python, Maths, Newton, NumPy and SciPy, PyXLL, UDFs | Tagged , , , , , , , , , , , | 1 Comment

Scipy Functions with Excel and pyxll 6 – Integration

The py_Integrate spreadsheet, with associated Python code in pyScipy3.py, are included in the download file:

py_SciPy.zip

The spreadsheet and associated Python packages have had significant edits since the first post in this series, so please download the latest file.

Details of the required pyxll package (including download, free trial, and full documentation) can be found at: pyxll

For those installing a new copy of pyxll, a 10% discount on the first year’s fees is available using the coupon code “NEWTONEXCELBACH10”.

The py_Integrate spreadsheet links to the Scipy quadrature functions. In addition, a Python version of the VBA Quad_Tanh_Sinh function by Graham Dennes has been added. See Tanh-Sinh Quadrature v. 5.03 for details and download of the VBA version.

Three functions allow for integration of functions with one variable:

The Tanh-Sinh version works with either a string on the spreadsheet or a Python function:

The Scipy and Tanh-Sinh functions return near identical resuts:

For typical functions the Tanh-Sinh function is a little slower than the Scipy functions, since it is written entirely in uncompiled code, but for functions requiring a large number of iterations it can be faster:

The spreadsheet includes examples with trigonometric functions, with results accurate to machine precision:

There are also functions linking to the Scipy functions for double, triple, and multiple integration, with examples of each:

Posted in Excel, Link to Python, Maths, Newton, Numerical integration, NumPy and SciPy, PyXLL, UDFs | Tagged , , , , , , , , , , | Leave a comment

Scipy Functions with Excel and pyxll 5 – Evaluate with Units

The Scipy series continues with the py_EvalU spreadsheet included in the download file:

py_SciPy.zip

The spreadsheet and associated Python packages have had significant edits since the first post in this series, so please download the latest file.

Details of the required pyxll package (including download, free trial, and full documentation) can be found at: pyxll

For those installing a new copy of pyxll, a 10% discount on the first year’s fees is available using the coupon code “NEWTONEXCELBACH10”.

The py_EvalU spreadsheet provides functions for the evaluation of functions entered as text, conversion and evaluation of units, and high precision maths functions. In addition to Numpy and Scipy, the following Python packages should be installed:

  • Sympy
  • Pint
  • MPMath
  • Matplotlib
  • Plotly

The Examples sheet has various functions for working with different units:

py_ConvertA converts column arrays between specified unit systems:

py_ToSIBase and py_FromSIBase convert between base SI units and a specified unit system:

The py_ConvertTab converts between unit systems in tabular format:

List_units lists all unit names in a specified system:

The functions below convert between metric and feet and inches in fraction format:

The py_EvalU function evaluates a text string, adjusting for the specified input and output units:

The Latex sheet displays the Plot_Math function, which converts a text string to Latex format, then uses Matplotlib to convert this to a graphic image. The original text string may be evaluated with the py_Eval or py_EvalU functions, or optionally Plot_Math may return the evaluated result (Cell C27 in the image below):

The Implied Units sheet shows two options for unit-aware evaluation of equations including factors with implied units:

The mpmath sheet shows examples of high precision calculations using the mp_Eval function. In the example below the sum of 3 cubes of 17 digit integers is correctly evaluated to 42, whereas floating point calculations return a value of 1.09785E+36.

Finally the Pint_Quant sheet shows the creation of Pint Quantity objects with specified units, and the creation of new units:

Posted in Excel, Link to Python, Maths, Newton, NumPy and SciPy, PyXLL, UDFs | Tagged , , , , , , , , , , , , , | 2 Comments

Scipy Functions with Excel and pyxll 4 – Solvers 2

Following the previous post I will continue to look at the py_Solvers spreadsheet included in the download file:

py_SciPy.zip

Details of the required pyxll package (including download, free trial, and full documentation) can be found at: pyxll

For those installing a new copy of pyxll, a 10% discount on the first year’s fees is available using the coupon code “NEWTONEXCELBACH10”.

This post will look at the py_SolveFS function for solving multi-variable equations using the Python root function.

Documentation is given on the spreadsheet:

The first example is a simple problem from the Scipy documentation:

This is followed by a more useful example, looking at the elastic design of a reinforced concrete section. (click on any image for full-screen view):

This is an extension of the second example for the py_Brent function in the previous post. In that case the area of the tension reinforcement was specified, and the depth of the section neutral axis was found for a specified bending moment and axial load. In today’s example only the number of tension bars is specified and the required outputs are the diameter of the tension bars, the strain at the compression face, and the depth of the neutral axis, for a specified bending moment, axial load and tensile stress in the tension reinforcement.

Note that the same results can be generated using the Excel solver:

To solve this problem with the Excel solver the square of the deviation of each return value from the target values is calculated on the spreadsheet, then the sum of the squares is minimised, The Excel solver solution is identical to the Python results, but it has several disadvantages:

  • It is slower
  • It takes longer to set up
  • It is less flexible and provides less control over solution methods
  • The solver results need to be recalculated every time an input is changed

The final example is a more complex problem from the Scipy docs (see the docs for full details):

Posted in Concrete, Excel, Link to Python, Maths, Newton, NumPy and SciPy, PyXLL, UDFs | Tagged , , , , , , , , , | 1 Comment

Scipy Functions with Excel and pyxll 3 – Solvers 1

Following the the first post in this series I will move on to the py_Solvers spreadsheet included in the download file:

py_SciPy.zip

Details of the required pyxll package (including download, free trial, and full documentation) can be found at: pyxll

For those installing a new copy of pyxll, a 10% discount on the first year’s fees is available using the coupon code “NEWTONEXCELBACH10”.

This post will look at functions for solving equations with one variable, and the next one multi-variable equations.

The py_Brent function finds a root to a function of one variable within a specified range using Brent’s Method:

The function may either be entered as text on the spreadsheet using the Python lambda format, or may be the name of any accessible Python function. In the example above func2 and func3 are included in the pyScipy3 module:

def func2(x):
    return -2*x**4 + 2*x**3 + -16*x**2 + -60*x + 100

def func3(x, a, b):
    return -2*x**4 + 2*x**3 + -16*x**2 + -a*x + b

The py_BrentA function allows two of the function arguments to be transferred as row and column arrays, with the result returned as a table:

In the first example the function has one unknown, T, two variable arguments, epsilon and alpha, and five fixed arguments, G_1 to G_5. 

The 5 fixed arguments have been converted to numerical values using the py_Eval function so that the unknown and the two variable arguments are the only arguments required for the py_BrentA function:

The second example finds the depth of the Neutral Axis of a reinforced concrete section with elastic properties with two variable arguments (applied axial force and bending moment) and 7 fixed arguments

For the on-sheet lambda function the fixed arguments must be converted to numerical values, again using py_Eval, but for the Python function the values can be passed to the function as an array:

def FindDNA(x, Ax, Mom, addargs):   
 
    A = addargs[0]
    B = addargs[1]
    C = addargs[2]
    D = addargs[3]
    E = addargs[4]
    F = addargs[5]
    G = addargs[6]
    
    ecc = Mom*1000/Ax
    
    return A*x**3+(B-C*ecc)*x**2+(D-ecc*E)*x+Mom*1000/Ax*F-G

On the spreadsheet either approach may be used by entering 1 or 2 in the “Func Type” cell (Y74).

The py_MinimizeFS function has similar functionality to py_Brent, but uses the Python minimize_scalar function, that allows alternative solver methods:

The py_MinimizeF function provides unconstrained or constrained minimization of scalar functions of one or more variables using the Python minimize function, using 11 alternative solver methods. 

The py_FindRoots function returns a root of a vector function.

Examples in the spreadsheet include the root finding example used for the py_Brent function, and more complex examples from the Scipy documentation:

Posted in Excel, Link to Python, Maths, Newton, NumPy and SciPy, PyXLL, UDFs | Tagged , , , , , , , , | Leave a comment