Posts

Showing posts from September, 2020

C++: Hidden array copy operation

A fixed-size array type in C++ does not have a copy constructor or a copy assignment. The following code won't compile: int a[2] = {1, 2}; int b[2] = a; A recommended way to fix this is to use std::array from the header <array> instead because it supports copying (and more): std::array<int, 2> a = {1, 2}; std::array<int, 2> b = a; Alternatively, one can resort to std::copy in <algorithm> , or the old school std::memcpy in <cstring> . Interestingly, the C++ language does have a way to copy fixed-size arrays, but it can only be accessed via a lambda capture. Here's an example that demonstrates that an array copy is actually done when it's captured by value into a lambda expression: (click this repl.it link if you want to play with the code) #include <functional> #include <iostream> using namespace std; struct LoudInt { int v; LoudInt(int v): v{v} { cout << "LoudIn...

Adding code snippets to Blogger posts (or any HTML pages)

As a preparation for writing a blog post about coding, I had to look for a way to show code snippets nicely on my post. I found highlight.js to be the best solution but the way to use it is not super straightforward. I'll explain how I use it on this blog post. Using highlight.js To "install" highlight.js, simply add the following code inside the <head> section: <link href='//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/styles/default.min.css' rel='stylesheet'/> <script src='//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/highlight.min.js'/> <script>hljs.initHighlightingOnLoad();</script> If you're using Blogger, this should be done in the theme html file, which you can get to by going to the "Theme" tab, clicking the "Customize" button, and choosing "Edit HTML". This is just like how I added MathJax to Blogger . After we have installed highlight.j...

Matrix Multiplication

In highschool, I was introduced to these operations on matrices: Add matrices. Multiply matrices. Compute the determinant of a matrix. Invert a matrix. Solve a system of linear equations with Cramer's rule . There were a lot of mysteries around why and how that did not get explained. Years passed and I entered college, yet those mysteries never were explained despite the frequent use of those operations. Here's a list of some mysteries that occupied my mind for quite some time: Why is matrix multiplication defined that way? Why is matrix multiplication associative ? (This means $A(BC) = (AB)C$.) Why is the determinant defined that way? Why does Cramer's rule work? Why is the matrix of cofactors defined that way? I eventually knew the answers, but I wish that I had had someone tell me sooner because such a simple explanation would suffice to answer all those questions. (Some of you might disagree on the use of ...

Re-starting my blogging habit - putting LaTeX in Blogger posts

I vaguely remember some joy I had when I was blogging somewhat religiously. The first topic I wrote about was my journey to understand what the matrix determinant was. When I looked back at those posts, I felt that they were silly, and that I was pretty stupid. I even felt embarrassed that I wrote those posts with so much excitement and confidence even though I knew so little. Was I so blinded by the Dunning-Kruger effect? Why would people even read what I wrote. I was not even close to being an expert on the matter. There were a lot of "better-written" textbooks out there, right? But actually, after spending a bit more time going through my past work, I realized that my posts were not totally pointless despite the strong sense of "Geez, you're so noob". In fact, it is the naivety that makes blog posts stand out among many of the "better-written" serious publications. It is my belief that when the writer is just a little less clueless than the reader a...