py_Numpy update 2: Polynomial functions

Continuing from the previous post, this post looks at the polynomial functions in the revised py_Numpy spreadsheet. The updated code and spreadsheet can be downloaded from:

 py_SciPy.zip

The available functions call the Numpy functions for solution and evaluation of polynomials, plus some additional functions with closed-form solutions for polynomials of up to quartic order. The available functions for finding the roots of polynomial equations are shown below (click on any image for full-size view):

Numpy iterative functions:

  • py_PolyRoots: Find the real and complex roots of a polynomial equation, calling the Numpy polyroots function. Coefficients are listed in increasing order of x.
  • py_PolyRoots: Provides an option for the order of coefficients. By default list in decrasing powers of x
  • py_PolyRootsC: Accepts complex values for the input coefficients. Also has an option to list coefficients in descending powers of x, default = ascending powers.
  • py_Polyroots2: Find the real and complex roots of a polynomial equation, calling the Numpy Polynomial function and .roots method.

Closed-form functions:

  • py_Quadratic: Find the real and complex roots of a quadratic equation: a x^2 + b x + c = 0
  • py_Cubic: Find the real roots of a cubic equation: a x^3 + b x^2 + c x + d = 0
  • py_CubicC: Find the real and complex roots of a cubic equation: a x^3 + b x^2 + c x + d = 0
  • py_CubicT: Find the real roots of a cubic equation: a x^3 + b x^2 + c x + d = 0, alternative solution method.
  • py_Quartic:  Find the real and complex roots of a quartic equation: a x^4 + b x^3 + c x62 + d X + e = 0

Calculation times for 100,000 iterations are shown in the screenshot below, with plain Python code, and with the Numba JIT compiler.

More functions:

  • py_PolyFromRoots: Return the polynomial with the given roots
  • py_PolyVal Evaluate a polynomial for a given x value
  • py_PolyFit Fit a polynomial function to XY data
  • py_PolyCompanion Returns the companion matrix to an array of polynomial coefficients in ascending order
  • py_PolyDer Differentiate a polynomial
  • py_PolyInt Integrate a polynomial
  • py_PolyAdd Add one polynomial to another
  • py_PolySub Subtract one polynomial from another
  • py_PolyMul Multiply one polynomial by another
  • py_PolyDiv Divide one polynomial by another
  • py_PolyPow Raise a polynomial to a power
Posted in Curve fitting, Excel, Link to Python, Maths, Newton, NumPy and SciPy, PyXLL, UDFs | Tagged , , , , , , | Leave a comment

Numpy where and split, and py_Numpy update

The py_Numpy spreadsheet and associated code has been updated, with two new functions, links to the Numpy on-line help updated, and updates to sorting and polynomial functions. The updated code and spreadsheet can be downloaded from:

Updated Numpy files included in: py_SciPy.zip

Note that the Numpy code comes in 2 versions: pyNumpy-noJIT.py and pyNumpy-jit.py. The pyNumpy-jit.py requires the Numba just-in-time compiler to be installed, which provides very much faster results for maths intensive operations, compared with plain Python code. The default pyNumpy.py module is currently a copy of the noJIT version.

The py_SciPy download contains full open-source Python code and example spreadsheets for a wide range of Scipy functions, as well as the Numpy functions covered in this post. For more details, see the series of posts starting at: Scipy functions with Excel and pyxll. Also see Python and pyxll for details of the pyxll add-in, required to link my Python code to Excel.

This post will look at the two new functions, py_Where and py_Split, which can be found on the Array Functions tab of the py_Numpy spreadsheet. The other changes will be covered in the following post.

The py_Where function calls the Numpy “where”:
numpy.where(condition, [xy, ]/)
Return elements chosen from x or y depending on condition.

The x, y return values are optional, and where omitted the function returns an array with the base 0 index values of the data values that satisfy the given condition, as shown in Column D below:

For this case the required inputs are:

  • the data range (a single column or row)
  • the condition, entered as text, one of <, <=, =, >=, or >
  • the limit value

The output is a single column array with the base 0 index of the data values that satisfy the condition, in this case those that are <= 13.

Where x and y are specified typical output is shown in Column G above. Where the condition is satisfied x (‘Hearts’) is returned, and otherwise y (‘Spades’).

The py_Split function splits the data into a number of smaller arrays at the specified locations:

The sub-arrays are returned as rows, with empty cells displaying #N/A.

The Numpy help on the function (and most of the other functions in py_Numpy) can be displayed by clicking on ‘Help on this function’ in the bottom-left corner of the function dialog box:

Posted in Arrays, Excel, Link to dll, Link to Python, PyXLL, UDFs | Tagged , , , , , , | 1 Comment

py_RC Elastic 1.05 with elastic biaxial bending

The py_RC Elastic spreadsheet (last discussed here) has been updated with a new function py_Biax, providing elastic analysis of reinforced concrete sections under combined axial load and biaxial bending. The new spreadsheet and associated Python code can be downloaded from:

py_RC Elastic.zip

The download file includes full open-source Python code. For details of the pyxll package required to link the Python code to Excel see: https://newtonexcelbach.com/python-and-pyxll/

The code and spreadsheet layout are based on the VBA version presented at: Elastic Biaxial Bending

Input for a T-section beam is shown in the screenshot below. The concrete is defined by the coordinates of each corner point, and reinforcement is is detailed in layers, with the coordinates of the ends of each layer:

The angle of the neutral axis is found by iteration, and the estimated angle should be entered in cell E5, then click the “Adjust NA Angle” button. The NA angle and position will be adjusted so that the stress at both ends of the NA is zero:

The function output displays:
Concrete, reinforcement and combined section properties:

Concrete stresses at each corner of the section in compression:

Reinforcement stresses at the ends of each layer:

Posted in Beam Bending, Concrete, Excel, Link to Python, Newton, PyXLL, UDFs | Tagged , , , , , , | Leave a comment

ConBeamU update

Following a comment at https://newtonexcelbach.com/2023/01/20/using-conbeamu/ I have updated the ConBeamU-Template file so that the function to select the input range works the same as in the main spreadsheet. For more details of how the spreadsheet works, see the link above, and to download the new file click on:

ConBeamU.zip

Posted in Beam Bending, Excel, Newton, UDFs, VBA | Tagged , , , , , , , | Leave a comment

Text from pictures in Excel

Excel recently added the ability to extract text from an image, either on the clipboard or from a selected file. To try this out I used a screenshot of a table with vertically aligned text, from a pdf copy of an AutoCAD file:

The procedure for importing the data is very straightforward. Select Get-Data From Picture on the Data Tab:

Select “Picture From Clipboard” and the process to detect and convert the text will start:

When complete it displays an image of the extracted text, with options to review or paste directly to the spreadsheet:

Unfortunately with vertical text the results were a little disappointing!

Rotating the image through 90 degrees (using IrfanView) the results were much better:

In the screenshot above the data in columns A to C was extracted from the image on the clipboard, which has been pasted in columns E to I. The results are still not perfect, in particular:

  • Some 1s at the start or end of a number have been missed.
  • Some zeros have been converted to o.
  • Spaces have been inserted into some numbers, usually associated with a 1.

In columns J to K the results have been converted to either numbers or #Value, using the Value function.

In columns N to P Value has been used in conjunction with Substitute, to remove any spaces inserted between numbers, so the result can be converted to valid numbers. Note that the results still need to be checked carefully, since there is no way to check where digits have been removed, other than a visual check of the original table.

Posted in Charts, Excel | Tagged , , , | Leave a comment