代码片段仓库

收集、管理和分享有用的代码片段,提高开发效率

代码片段

卡尔曼滤波器 Go

状态估计算法

package main

import "math"

type Kalman struct {
    X          []float64  // State vector x
    P          [][]float64 // State covariance
    F, B, H [][]float64 // Transition matrices
}

func (k *Kalman) Predict(u []float64) {
    // x_pred = F*x + B*u
    n := len(k.X)
    newX := make([]float64, n)
    for i := 0; i < n; i++ {
        for j := 0; j < n; j++ {
            newX[i] += k.F[i][j] * k.X[j]
        }
        for j := 0; j < len(u); j++ {
            newX[i] += k.B[i][j] * u[j]
        }
    }
    k.X = newX
    // P = F*P*F^T + Q
}
卡尔曼滤波器 Go

状态估计算法

package main

import "math"

type Kalman struct {
    X          []float64  // State vector x
    P          [][]float64 // State covariance
    F, B, H [][]float64 // Transition matrices
}

func (k *Kalman) Predict(u []float64) {
    // x_pred = F*x + B*u
    n := len(k.X)
    newX := make([]float64, n)
    for i := 0; i < n; i++ {
        for j := 0; j < n; j++ {
            newX[i] += k.F[i][j] * k.X[j]
        }
        for j := 0; j < len(u); j++ {
            newX[i] += k.B[i][j] * u[j]
        }
    }
    k.X = newX
    // P = F*P*F^T + Q
}
卡尔曼滤波器 Go

状态估计算法

package main

import "math"

type Kalman struct {
    X          []float64  // State vector x
    P          [][]float64 // State covariance
    F, B, H [][]float64 // Transition matrices
}

func (k *Kalman) Predict(u []float64) {
    // x_pred = F*x + B*u
    n := len(k.X)
    newX := make([]float64, n)
    for i := 0; i < n; i++ {
        for j := 0; j < n; j++ {
            newX[i] += k.F[i][j] * k.X[j]
        }
        for j := 0; j < len(u); j++ {
            newX[i] += k.B[i][j] * u[j]
        }
    }
    k.X = newX
    // P = F*P*F^T + Q
}
遗传编程 Go

符号回归实现

package main

type Node struct {
    op     string
    left, right *Node
}

fung eval(n *Node, x float64) float64 {
    switch n.op {
    case "x":
        return x
    case "+":
        return eval(n.left, x) + eval(n.right, x)
    case "*":
        return eval(n.left, x) * eval(n.right, x)
    default:
        return 0
    }
}

func geneticProgramming(data map[float64]float64, popSize, gens int) interface{} {
    pop := make([]*Node, popSize)
    for i := range pop {
        pop[i] = randTree(3)
    }

    for gen := 0; gen < gens; gen++ {
        fitness := make([]float64, popSize)
        for i, ind := range pop {
            sumSq := 0.0
            for x, y := range data {
                pred := eval(ind, x)
                sumSq += (pred - y) * (pred - y)
            }
            fitness[i] = 1.0 / (1.0 + sumSq)
        }

        // Selection, crossover, mutation logic here
    }
    return bestIndividual()
}
卡尔曼滤波器 Go

状态估计算法

package main

import "math"

type Kalman struct {
    X          []float64  // State vector x
    P          [][]float64 // State covariance
    F, B, H [][]float64 // Transition matrices
}

func (k *Kalman) Predict(u []float64) {
    // x_pred = F*x + B*u
    n := len(k.X)
    newX := make([]float64, n)
    for i := 0; i < n; i++ {
        for j := 0; j < n; j++ {
            newX[i] += k.F[i][j] * k.X[j]
        }
        for j := 0; j < len(u); j++ {
            newX[i] += k.B[i][j] * u[j]
        }
    }
    k.X = newX
    // P = F*P*F^T + Q
}
粒子群优化 Go

群体智能算法

package main

type Particle struct {
    position []float64
    velocity []float64
    bestPos   []float64
    bestCost float64
}

func PSO(obj func(pos []float64) float64, dim, numParticles, maxIter int) []float64 {
    particles := make([]*Particle, numParticles)
    globalBest := make([]float64, dim)
    globalCost := math.MaxFloat64

    for _, p := range particles {
        position := make([]float64, dim)
        for i := range dim {
            position[i] = rand.Float64() * 100.0
        }
        p.position = position
        p.bestCost = math.MaxFloat64
        p.bestPos = make([]float64, dim)
        copy(p.bestPos, p.position)
    }

    for iter := 0; iter < maxIter; iter++ {
        for _, p := range particles {
            cost := obj(p.position)
            if cost < p.bestCost {
                p.bestCost = cost
                copy(p.bestPos, p.position)
            }
            if cost < globalCost {
                globalCost = cost
                copy(globalBest, p.position)
            }
        }
    }
    return globalBest
}

为什么选择CodeSnippets?

高效管理您的代码片段,提高开发效率

智能搜索

通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能

语法高亮

支持多种编程语言的语法高亮,使代码更加清晰易读

多设备同步

随时随地访问您的代码片段库,支持桌面和移动设备

热门分类

浏览最受欢迎的代码分类