{"cells": [{"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["#! /usr/bin/env python3\n", "#\n", "def linalg_solve_bench ( n, verbose = True ):"]}, {"cell_type": "markdown", "metadata": {}, "source": ["****************************************************************************80
\n", "
\n", " linalg_solve_bench() uses scipy.linalg.solve() for the LINPACK benchmark.
\n", "
\n", " Licensing:
\n", "
\n", " This code is distributed under the MIT license.
\n", "
\n", " Modified:
\n", "
\n", " 07 February 2025
\n", "
\n", " Author:
\n", "
\n", " John Burkardt.
\n", "
\n", " Input:
\n", "
\n", " integer N, the order of the matrix.
\n", ""]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [" from scipy.linalg import solve\n", " from time import perf_counter\n", " import numpy as np\n", " import platform"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [" if ( verbose ):\n", " print ( '' )\n", " print ( 'linalg_solve_bench():' )\n", " print ( ' python version: ' + platform.python_version ( ) )\n", " print ( ' numpy version: ' + np.version.version )\n", " print ( ' Test scipy.linalg.solve() on the LINPACK benchmark.' )\n", " print ( ' Datatype: float64' )\n", " print ( ' Matrix order N = ', n )\n", "#\n", "# Set the matrix A, a solution x, and a right hand side.\n", "#\n", " A = 2.0 * np.random.random ( [ n, n ] ) - 1.0\n", " x_exact = np.ones ( n )\n", " b = np.matmul ( A, x_exact )\n", "#\n", "# Solve the system.\n", "#\n", " cpu = perf_counter ( )\n", " x = solve ( A, b )\n", " cpu = perf_counter ( ) - cpu\n", "#\n", "# Compute residual.\n", "#\n", " r = np.matmul ( A, x ) - b\n", "#\n", "# Take infinity norms.\n", "#\n", " a_norm = np.linalg.norm ( A, np.inf )\n", " r_norm = np.linalg.norm ( r, np.inf )\n", " x_norm = np.linalg.norm ( x, np.inf )\n", "#\n", "# Report.\n", "#\n", " eps = np.finfo(float).eps\n", " ratio = r_norm / ( a_norm * x_norm * eps )"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [" ops = ( 2 * n * n * n ) / 3.0 + 2.0 * n * n\n", " mflops = ops / ( 1000000 * cpu )\n", " unit = 2.0 / mflops"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [" cray = 0.056\n", " cray_ratio = cpu / cray"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [" if ( verbose ):\n", " print ( '' )\n", " print ( ' Normalized residual = ', ratio )\n", " print ( ' Residual norm = ', r_norm )\n", " print ( ' Machine epsilon = ', eps )\n", " print ( ' First X[] = ', x[0] )\n", " print ( ' Last X[] = ', x[-1] )\n", " print ( ' CPU seconds = ', cpu )\n", " print ( ' MegaFLOPS = ', mflops )\n", " print ( ' Unit = ', unit )\n", " print ( ' Cray ratio = ', cray_ratio )\n", "#\n", "# Terminate.\n", "#\n", " if ( verbose ):\n", " print ( '' )\n", " print ( 'linalg_solve_bench():' )\n", " print ( ' Normal end of execution.' )"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [" return mflops"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["def timestamp ( ):"]}, {"cell_type": "markdown", "metadata": {}, "source": ["****************************************************************************80
\n", "
\n", " timestamp() prints the date as a timestamp.
\n", "
\n", " Licensing:
\n", "
\n", " This code is distributed under the MIT license.
\n", "
\n", " Modified:
\n", "
\n", " 06 April 2013
\n", "
\n", " Author:
\n", "
\n", " John Burkardt
\n", ""]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [" import time"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [" t = time.time ( )\n", " print ( time.ctime ( t ) )"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [" return"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["if ( __name__ == '__main__' ):\n", " timestamp ( )\n", " n = 1000\n", " linalg_solve_bench ( n )\n", " timestamp ( )"]}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4"}}, "nbformat": 4, "nbformat_minor": 2}