# Vectorization over C

The title is probably misleading, but this is a lesson I needed to talk about.

I wrote out some simple code for the quadrature over the reference triangle last time, which involves a double loop. To my chagrin, my immediate reaction to speeding up the code was to put it into Cython, and give it some type declaration.

This did speed up my integrals, but not as much as vectorization. By simply condensing one of the loops into a dot product, and using vector-function evaluation, I sped up my code a substantial amount, especially with higher order integration of “hard” functions.

``def quadtriangle_vector(f, w00, w10, x00, x10):    total = 0    for i in range(len(w00)):        total += w00[i] * np.dot(w10 / 2, f([(1 + x00[i]) * (1 - x10) / 2 - 1, x10]))    return total``

To see what I mean, consider the following function

``from scipy.special import eval_jacobi as jacdef f(x):    return jac(2, 1, 1, np.sin(x[0] - x[1]))p = 20x00, w00 = rj(p + 1, 0, 0)x10, w10 = rj(p + 1, 1, 0)``

The speedup I get is staggering.

``````Also, I tried to fully vectorize by removing the outer-loop. This actually slowed down the code a bit. Maybe I did it wrong? But for now, I’m decently happy with the speed.
``````
``` CategoriesMath, Programming ```
``` Leave a Reply Your email address will not be published. Required fields are marked *Comment * Name * Email * Website Δdocument.getElementById( "ak_js_1" ).setAttribute( "value", ( new Date() ).getTime() ); This site uses Akismet to reduce spam. Learn how your comment data is processed. Post navigation Previous PostPrevious Quadrature Rules on TrianglesNext PostNext Gradient of the Barycentric Coordinate in 2D ```
``` ```
``` Currently “_______” Reading: New Yorker magazines… Watching: Maniac Playing: LoL Recent Posts Tale of a Golden Corral My (Not-So-Successful) Quest to Conquer the NYT Connections Game with Word2Vec Tapping Out (no title) A Terrible Philosophy Search for: Archives Archives Select Month July 2024 February 2024 January 2024 November 2023 September 2023 June 2023 March 2023 February 2023 January 2023 December 2022 November 2022 September 2022 August 2022 July 2022 June 2022 May 2022 April 2022 March 2022 February 2022 January 2022 December 2021 November 2021 October 2021 September 2021 August 2021 July 2021 June 2021 May 2021 April 2021 January 2021 December 2020 November 2020 October 2020 September 2020 August 2020 July 2020 June 2020 May 2020 April 2020 September 2019 August 2019 May 2019 April 2019 February 2019 January 2019 November 2018 October 2018 September 2018 August 2018 July 2018 June 2018 May 2018 April 2018 March 2018 February 2018 January 2018 August 2017 July 2017 June 2017 April 2017 March 2017 February 2017 January 2017 December 2016 January 2016 September 2015 August 2015 July 2015 June 2015 March 2015 February 2015 January 2015 December 2014 November 2014 September 2014 August 2014 July 2014 June 2014 May 2014 April 2014 March 2014 February 2014 January 2014 August 2013 July 2012 June 2012 May 2012 /* <![CDATA[ */ (function() { var dropdown = document.getElementById( "archives-dropdown-2" ); function onSelectChange() { if ( dropdown.options[ dropdown.selectedIndex ].value !== '' ) { document.location.href = this.options[ this.selectedIndex ].value; } } dropdown.onchange = onSelectChange; })(); /* ]]> */ Fixtures About me Software Academic Uploads Marshall’s Travel Service ```
``` ```
``` Proudly powered by WordPress ```
``` ```
``` var twentyseventeenScreenReaderText = {"quote":"<svg class=\"icon icon-quote-right\" aria-hidden=\"true\" role=\"img\"> <use href=\"#icon-quote-right\" xlink:href=\"#icon-quote-right\"><\/use> <\/svg>","expand":"Expand child menu","collapse":"Collapse child menu","icon":"<svg class=\"icon icon-angle-down\" aria-hidden=\"true\" role=\"img\"> <use href=\"#icon-angle-down\" xlink:href=\"#icon-angle-down\"><\/use> <span class=\"svg-fallback icon-angle-down\"><\/span><\/svg>"}; ```