0%

Since C++11, std::decay is introduced along with <type_traits>. It is used to decay a type, or to convert a type into its corresponding by-value type. It will remove any top-level cv-qualifiers(const, volatile) and reference qualifiers for the specified type. For example, int& is turned into int and an array type becomes a pointer to its element types. Knowing its usage, we could try to implement our own version of std::decay.

Read more »

In C++, even though a function generated from a function template has the same name and the same type as the ordinary function, they are never equivalent. Besides, the non-template function is more preferred:

1
2
3
4
5
6
7
8
template <typename T>
void foo(T) { std::cout << "template foo()" << std::endl; }
void foo(int) { std::cout << "ordinary foo()" << std::endl; }

int main() {
foo(5); // print "oridinary foo()"
return 0;
}

On top of that, it is not easy to templify a copy/move constructor because the compiler may implicitly define a copy/move constructor.

Read more »

Support vector machine (SVM) plays an important role in machine learning. Actually, SVM is one of my favorite models because of its analytical property. Its main idea is to find the optimal hyperplane that can linearly separate the data, or maximise margin in the feature space. It is one of the most robust models based on statistical learning framework in VC theory (Vapnik–Chervonenkis theory). The determination of the model parameters is a quadratic programming problem, or convex optimisation more specifically. Its solution is usually sparse, and the new input prediction depends only on the evaluation of a subset of training data with kernel function. One of the most common and efficient approaches to train SVM is sequential minimal optimisation (SMO), which breaks down the problem into solving a pair of parameters analytically at each step. Besides, SMO also eliminates the need for matrix storage issue when the training data size is huge.

Read more »

When we develop our program or the system continues to grow as time goes by, memory leakage is usually a pain we suffer most. To militate against this problem, C++ has introduced smart pointer family - unique_ptr, shared_ptr, weak_ptr, defined in header <memory>, since C++11.

Precisely, unique_ptr inherits most of characteristics from auto_ptr, which has been implemented since C++03. Yet, due to the lack of move semantics, it encounters significant limitation:

1
2
3
4
5
auto_ptr<int> p (new int(1));
vector<auto_ptr<int>> vec;

vec.push_back(p); // !!! error, because of Copy constructor
vec.push_back(std::move(p)) // Okay, with move semantics since C++11
Note that auto_ptr is deprecated now and should not be used any more for safety.

Read more »

C++ provides a plethora of containers that allow us to dynamically allocate our elements in run time. Take vector as an example. It is widely used in many applications. However, for some extreme cases, we may still want to avoid the overhead of reallocation or copy operations when those operations are quite expensive.

Since C++11, with the advent of emplace methods, developers are able to pursue more efficiency via them.

Read more »

Brownian motion is a phenomenon that particles in the seemingly motionless liquid are still undergone unceasing collisions in an erratic way. It was firstly observed by Robert Brown in 1827. In 1923, Norbert Wiener had attempted to formulate this observations in mathematical terms; thus it is also known as Wiener process.

Owing to its randomness, Brownian motion has a wide range of applications, ranging from chaotic oscillations to stock market fluctuations. In this article, I will describe its basic property and how to visualise it and its variants with Python.

Read more »

每一次重新安裝好用來開發的Linux環境時,總會因為自己是使用較小眾的行列輸入法,常得在過程中的安裝和設定上掙扎許久...

以往這些紀錄都只會留存在我個人的 Evernote;受惠於網路甚多,現在決定開始把這些過程重新整理並分享給有需要的人

以下步驟也適用於倉頡、大易、速成、嘸蝦米等中文輸入法

Read more »