## A Python Riddle

From the book Fluent Python (which you can get from the Humble Bundle right now):

What would the following piece of code do?

``t = (1, 2, [30, 40])t[2] += [50, 60]``

Four choices:

1. `t` becomes `(1,2,[30,40,50,60])`
2. A TypeError is raised because tuples does not support item assignments?
3. Neither
4. Both 1. and 2.

The answer is the link, which is quite surprising.

It turns out that `t[2].extend([50, 60])`doesn’t break Python, and this riddle is really a super esoteric corner case…

## 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.
``````
``` ```
``` Posted on April 18, 2017The (lack of a) Matrix I think I finally understand why software packages like PETSc has an option for an operator when doing something like conjugate gradient. Why isn’t having a matrix good enough for everyone? Well turns out that while all linear operators can be translated to a matrix, it may not be the best way to represent the operator. As an example, consider a basis transformation from Bernstein polynomials to Jacobi (or vice versa). It’s certainly possible to find and construct a matrix which does the operation, but it’s ugly. On the other hand, it’s not that bad to write a code which utilizes the properties of the polynomials and convert it within in O(n^2) time. The key is that a Jacobi polynomial is a sum of Bernsteins, and Bernsteins can be degree raised or dropped at will. This function will outperform the matrix in many sense. For one, there’s no need to construct a matrix, which will take n^2 operations in the first place. Next, matrix multiplication will take a n^3 operation, so if we optimize enough, we will always beat it. Finally, it’s really less painful to code, because each line of the function serves a visible purpose. Anyways, I’m sold. (I’ll eventually publish the code in the summer) Posted on April 5, 2014Notes: SSD edition Some notes from the past week: It is incredibly easy to be an impostor in a more academic party. First of all, most of the people will be already intoxicated to the point where bullshit science can’t be discerned from actual science. This is good as I can just say random facts I remember from Popular Science.Another acceptable thing to do is to just ask questions upon questions. “What’s your research? … Oh that’s so cool! Tell me more about it! … So does this connect to insert scientific news here? Wow.” That’ll burn around 5 minutes minimum.The main problem comes when you run out of questions in the initial barrage. It also fails when the person is laconic or can’t speak English. Installing a SSD is extremely easy, but installing operating systems are hard. Right now, I have around 8 entries on my GRUB menu before I migrate everything over to my new distro.I followed the mount guide provided here, which seems intuitive enough on where to put mount points. I’ve also learned that mount and df -h are my friends. There’s also that good GParted software. The Lloyd Trefethren numerical linear algebra book is quite good for a quick overview of the subject. It doesn’t get bogged down with the analysis, and generally refers to other books (mainly the Van Loan) throughout. Holy shit URF mode. I need to be more brave in a certain subject…. Posted on March 29, 2014March 29, 2014Ray Casting with JOGL I won’t post the entire code here, because it’s pretty damn ugly. But here’s what I ended up doing: I used the gluunproject statement to find the beginning and end points to extrapolate a line from. Now that I have a line, I use the formula provided by Wikipedia using the vector formulation of the line. Simply do a loop over the vertices and find the minimum. Sorry for note posting recently… I got caught up in things… 🙁 Posted on March 21, 2014March 21, 2014VTune Profiler Error: “The Data Cannot be displayed, there is no viewpoint available for data ” The solution to this if you’re using the GUI on Linux can quite possibly be that ptrace_scope was set to 1. From the Intel forums: Note: In Ubuntu 11.4, you may need to disable ptrace_scope. cat /proc/sys/kernel/yama/ptrace_scope; “0” is expected, if it is “1”, Then do \$echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope Took me a while to find… hope this saves someone some time. Posted on March 17, 2014Hash Doing another SPH implementation for parallel computing. It’s amazing how fast adding a hash table, instead of looking at all particles does for one’s speed. 1429.22 seconds to only 327 seconds. That’s 5 times as fast! Posted on February 27, 2014February 27, 2014Cool… So I’ve decided this semester, we’re going to implement the entire Nvidia PhysX library and do particle-based dynamics next. – CS5643 Posted on February 26, 2014Wine IE Installs Wine to play Hearthstone. Made a gif for a project. Double clicked gif to see how it turned out. gif opened in IE under Wine. Posted on February 23, 2014Life’s Update I’ve been working on a few things: Position-based fluids for my CS5643 class, which is a pain to debug. Normally, I have tons of intuition on what numbers are suppose to be but my intuition is nil here. I’m unsure as we’re guessing the correct parameters or even the right formulas and procedure.It’s a great class, just the project are incredibly tedious. For example, my particles right now just seem to float of into the x direction and stay there. Why would it do that? Hopefully, what ends up happening is something like the following, except less pretty. I finally installed Hearthstone for my Elementary OS Linux box. It was incredibly easy following this tutorial. In terms of research, I’ve been reading up on different ways to generate the Krylov basis for GMRES as described in this paper. I’ll probably write up and refined the old  “idiot’s guide” again for this paper information (and fix the bad latex in there). This soundtrack (not allowed to embed). My card handeling has suffered :(. Posts navigation Page 1 Page 2 Next page ```
``` ```
``` Currently “_______” Reading: Never Let Me Go Watching: Succession Playing: BG3 Recent Posts Piles Slurping up SLERP Tale of a Golden Corral My (Not-So-Successful) Quest to Conquer the NYT Connections Game with Word2Vec Tapping Out Search for: Archives Archives Select Month September 2024 August 2024 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>"}; ```