Skip to content

Instantly share code, notes, and snippets.

View ggcr's full-sized avatar

Cristian Gutiérrez ggcr

  • Barcelona Supercomputing Center
  • Barcelona, Catalonia, Spain
View GitHub Profile
@ggcr
ggcr / insertion_sort.cpp
Last active September 29, 2024 14:06
Insertion sort C++
#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>
/*
O(n^2) in avg and worst.
*/
// Implementation using std::find_if
@ggcr
ggcr / bubble_sort.cpp
Last active May 10, 2024 09:42
Bubble sort C++, space complexity O(n)
#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>
/*
O(n^2) in avg and worst.
Can be done easily at space complexity O(n).
*/
@ggcr
ggcr / merge_sort.cpp
Created May 10, 2024 09:12
C++ Merge Sort
#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>
// Implementation
void print_vector(std::vector<int> array, std::string title) {
cout << title << ": " << "[";
for(int i : array)
@ggcr
ggcr / eigenfaces.py
Created August 6, 2023 11:25
Eigenfaces on the Olivetti faces dataset
import torch
import numpy as np
from PIL import Image
from torchvision import transforms
from sklearn.datasets import fetch_olivetti_faces
faces, _ = fetch_olivetti_faces(data_home='../data/', return_X_y=True, shuffle=True, random_state=np.random.RandomState(0))
faces = torch.from_numpy(faces)
n = faces.shape[0]
@ggcr
ggcr / main.py
Created August 5, 2023 10:40
Univariate Linear Regression using the solution of the Normal Equations for the Least Square problem.
import numpy as np
x = np.array([-0.5, -0.43, -0.23, 0.12, 0.40, 0.71, 1]).reshape(-1, 1)
y = np.array([-2, -0.2, 0.1, 0.83, 1.4, 0.98, 2.2]).reshape(-1, 1)
# Add a column of ones to the input data for the intercept term
X = np.concatenate((np.ones_like(x), x), axis=1)
xxT = X.T.dot(X)
inv = np.linalg.inv(xxT)