Skip to content

Instantly share code, notes, and snippets.

View ZJUGuoShuai's full-sized avatar

Guo Shuai ZJUGuoShuai

  • ByteDance
  • Hangzhou, China
  • 13:28 (UTC +08:00)
View GitHub Profile
@ZJUGuoShuai
ZJUGuoShuai / dft.swift
Created July 28, 2023 15:04
Swift 实现 DFT(原始算法)
import Cocoa
let input: [Float] = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
var output_real = Array<Float>(repeating: 0, count: 8)
var output_imag = Array<Float>(repeating: 0, count: 8)
for i in 0..<8 {
var real_sum: Float = 0.0
var imag_sum: Float = 0.0
for k in 0..<8 {

ComputeShader class 增加以下三个方法:

// 从 OpenCV Mat 创建(只读的) Metal Texture
- (id<MTLTexture>) textureFromCVMat:(const void*)cv_data Width:(int) w Height:(int)h {
  // Create a Metal texture descriptor
  MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR8Unorm
                                                                                               width:w
                                                                                              height:h
                                                                                           mipmapped:NO];
@ZJUGuoShuai
ZJUGuoShuai / epsilon.cc
Created August 8, 2023 08:57
Machine Epsilon 的作用
#include <limits>
#include <iostream>
int main() {
float eps = std::numeric_limits<float>::epsilon();
float a = 1.0f;
float b = a + eps;
float c = a + eps / 2.0f;
std::cout << "a == b: " << std::boolalpha << (a == b) << "\n"; // false
std::cout << "a == c: " << std::boolalpha << (a == c) << "\n"; // true
@ZJUGuoShuai
ZJUGuoShuai / bilinear_resize.py
Last active August 23, 2023 16:53
Bilinear Resize Implementation (Python)
def sample(img, x: float, y: float):
h, w = img.shape
left = math.floor(x)
top = math.floor(y)
right = left + 1
bottom = top + 1
# top left corner
if (0 <= left < w) and (0 <= top < h):
@ZJUGuoShuai
ZJUGuoShuai / myVector.cpp
Last active August 19, 2023 16:00
My implementation of std::vector
template <typename T>
class vector {
size_t size_ = 0;
size_t capacity_ = 0;
T* data_ = nullptr;
public:
// Default constructor
vector() = default;
@ZJUGuoShuai
ZJUGuoShuai / STL-Swap.md
Last active August 18, 2023 11:45
C++ STL 中的 std::swap 通常是如何实现的?

C++ STL 中的 std::swap 通常是如何实现的?

C++11 之前,只能使用拷贝构造和拷贝赋值:

template <typename T>
void swap(T& a, T& b) {
  T t = a;
  b = a;
 b = t;
@ZJUGuoShuai
ZJUGuoShuai / thread-safe-singleton.md
Last active August 19, 2023 04:03
C++ 如何实现线程安全的单例模式

基本版本

class Singleton {
  Singleton() = default;

 public:
  Singleton(const Singleton&)            = delete;
  Singleton& operator=(const Singleton&) = delete;
@ZJUGuoShuai
ZJUGuoShuai / showOpenCVMat.mm
Created August 20, 2023 09:02
Display a OpenCV Mat using a UIImageView.
void showOpenCVMat(cv::Mat img, UIImageView* imageView) {
cv::Mat converted;
img.convertTo(converted, CV_8U);
// convert the cv::Mat to UIImage
NSData *data = [NSData dataWithBytes:converted.data length:converted.elemSize() * converted.total()];
CGColorSpaceRef colorSpace =
CGColorSpaceCreateDeviceGray(); // CGColorSpaceCreateDeviceRGB();
CGDataProviderRef provider =
CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
CGImageRef imageRef = CGImageCreate(
@ZJUGuoShuai
ZJUGuoShuai / fuck-matplotlib.py
Created September 26, 2023 13:29
FUCK MATPLOTLIB CHINESE DISPLAY
""" 如何让 Matplotlib 正确显示中文 """
""" 第一步:通过字体路径创建一个字体属性对象 """
from matplotlib.font_manager import FontProperties
my_font = FontProperties(fname='/Path/To/Your/Font.ttf')
""" 第二步:在所有需要显示中文的绘图命令中增加 fontproperties=my_font 或 prop=my_font 参数 """
plt.xlabel('线程池线程数', fontproperties=my_font)
@ZJUGuoShuai
ZJUGuoShuai / conv1d-bmm.py
Created October 30, 2023 07:00
批次矩阵乘法 等价于一种特殊的 Conv1D
"""
表明:批次矩阵乘法 等价于一种特殊的 Conv1D
"""
import torch
N = 2
Cin = 3
Lin = 4
Cout = 5