Initial commit
This commit is contained in:
32
skills/julia-hydrotools/SKILL.md
Normal file
32
skills/julia-hydrotools/SKILL.md
Normal file
@@ -0,0 +1,32 @@
|
||||
---
|
||||
name: julia-hydrotools
|
||||
description: 计算短波辐射、长波辐射、潜在蒸散发、日出日落时间、湿度的基本变量处理。
|
||||
---
|
||||
|
||||
# 1 运行环境说明
|
||||
|
||||
- 在Julia中运行
|
||||
|
||||
- 在julia中首先加载包,`using HydroTools`
|
||||
|
||||
- 若没有包加载出错,则安装之,`using Pkg; Pkg.add("HydroTools")`
|
||||
|
||||
|
||||
## 1.1 函数说明
|
||||
|
||||
- `cal_Rsi_toa(lat, J)`: daily extraterrestrial radiation in MJ m-2 day-1
|
||||
+ `lat`: latitude in deg
|
||||
+ `J`: doy of year
|
||||
> 注意lat和J是scalar
|
||||
> 如果是vector,按照Julia的语法,采用`cal_Rsi_toa.(lat, J)`调用
|
||||
+ 默认返回单位是`MJ d-1`,若想转为`W m-2`,需要调用[MJ2W]函数,告诉用户返回的数字单位
|
||||
|
||||
|
||||
## 1.2 文件保存
|
||||
|
||||
文件保存采用Julia包`DataFrames`,`RTableTools`
|
||||
|
||||
```julia
|
||||
using RTableTools
|
||||
fwrite(df, "out.csv") # df is a DataFrame
|
||||
```
|
||||
22
skills/julia-hydrotools/examples.jl
Normal file
22
skills/julia-hydrotools/examples.jl
Normal file
@@ -0,0 +1,22 @@
|
||||
using HydroTools
|
||||
using Dates
|
||||
|
||||
lat = 20.0
|
||||
doy = 120
|
||||
|
||||
ws = HourAngleSunSet(lat, doy)
|
||||
# doy
|
||||
cal_Rsi_toa(lat, doy)
|
||||
|
||||
# date
|
||||
date = Date(2010, 6, 12)
|
||||
doy = dayofyear(date)
|
||||
cal_Rsi_toa(lat, doy)
|
||||
|
||||
# datetime
|
||||
time = DateTime(2010, 6, 12)
|
||||
doy = dayofyear(date)
|
||||
Rsi = cal_Rsi_toa(lat, doy) # [MJ d-1 m-2]
|
||||
|
||||
|
||||
MJ2W(Rsi) # [MJ d-1 m-2] to [W m-2]
|
||||
120
skills/julia-numerical/SKILL.md
Normal file
120
skills/julia-numerical/SKILL.md
Normal file
@@ -0,0 +1,120 @@
|
||||
---
|
||||
name: julia-numerical
|
||||
description: Execute numerical calculations and mathematical computations using Julia. Use this skill for matrix operations, linear algebra, numerical integration, optimization, statistics, and scientific computing tasks.
|
||||
---
|
||||
|
||||
# Julia Numerical Calculation Skill
|
||||
|
||||
This skill enables you to execute numerical calculations using Julia, a high-performance programming language designed for numerical and scientific computing.
|
||||
|
||||
## When to Use
|
||||
|
||||
Use this skill when you need to:
|
||||
- Perform matrix operations and linear algebra
|
||||
- Solve differential equations
|
||||
- Execute numerical integration or optimization
|
||||
- Calculate statistical measures
|
||||
- Handle large-scale numerical computations
|
||||
- Work with complex mathematical operations
|
||||
|
||||
## Setup
|
||||
|
||||
Before using this skill, ensure Julia is installed on your system:
|
||||
|
||||
```bash
|
||||
# On macOS (using Homebrew)
|
||||
brew install julia
|
||||
|
||||
# On Linux (Ubuntu/Debian)
|
||||
sudo apt-get install julia
|
||||
|
||||
# On Windows (using Chocolatey)
|
||||
choco install julia
|
||||
|
||||
# Or download from https://julialang.org/downloads/
|
||||
```
|
||||
|
||||
## Basic Examples
|
||||
|
||||
### Linear Algebra
|
||||
|
||||
```julia
|
||||
using LinearAlgebra
|
||||
|
||||
# Create matrices
|
||||
A = [1 2; 3 4]
|
||||
B = [5 6; 7 8]
|
||||
|
||||
# Matrix multiplication
|
||||
C = A * B
|
||||
|
||||
# Eigenvalues and eigenvectors
|
||||
eigenvals, eigenvecs = eigen(A)
|
||||
|
||||
# Matrix inverse
|
||||
A_inv = inv(A)
|
||||
```
|
||||
|
||||
### Numerical Integration
|
||||
|
||||
```julia
|
||||
using QuadGK
|
||||
|
||||
# Define a function
|
||||
f(x) = sin(x) * exp(-x)
|
||||
|
||||
# Integrate from 0 to ∞
|
||||
result, error = quadgk(f, 0, Inf)
|
||||
```
|
||||
|
||||
### Optimization
|
||||
|
||||
```julia
|
||||
using Optim
|
||||
|
||||
# Define objective function
|
||||
f(x) = (x[1] - 2)^2 + (x[2] - 3)^2
|
||||
|
||||
# Minimize
|
||||
result = optimize(f, [0.0, 0.0])
|
||||
```
|
||||
|
||||
### Statistics
|
||||
|
||||
```julia
|
||||
using Statistics
|
||||
|
||||
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
|
||||
# Statistical measures
|
||||
mean_val = mean(data)
|
||||
std_val = std(data)
|
||||
var_val = var(data)
|
||||
median_val = median(data)
|
||||
```
|
||||
|
||||
## How to Use This Skill
|
||||
|
||||
When you ask me to perform a numerical calculation:
|
||||
1. I'll identify the appropriate Julia packages needed
|
||||
2. Write Julia code to solve the problem
|
||||
3. Execute the code
|
||||
4. Return results and explanations
|
||||
|
||||
## Common Julia Packages
|
||||
|
||||
- **LinearAlgebra**: Matrix operations and linear algebra
|
||||
- **Statistics**: Statistical functions
|
||||
- **QuadGK**: Numerical integration
|
||||
- **Optim**: Optimization algorithms
|
||||
- **DifferentialEquations**: Solving differential equations
|
||||
- **Plots**: Visualization
|
||||
- **Distributions**: Probability distributions
|
||||
- **Random**: Random number generation
|
||||
|
||||
## Notes
|
||||
|
||||
- Julia is JIT-compiled, so first runs may include compilation time
|
||||
- Use `.jl` files for organizing longer scripts
|
||||
- Install packages with `using Pkg; Pkg.add("PackageName")`
|
||||
- Results are returned as Julia objects that are converted to readable format
|
||||
155
skills/julia-numerical/examples.jl
Normal file
155
skills/julia-numerical/examples.jl
Normal file
@@ -0,0 +1,155 @@
|
||||
# Julia Numerical Calculation Examples
|
||||
# This file contains common numerical computation patterns
|
||||
|
||||
# ============================================================================
|
||||
# Linear Algebra Examples
|
||||
# ============================================================================
|
||||
|
||||
function linear_algebra_examples()
|
||||
using LinearAlgebra
|
||||
|
||||
println("=== Linear Algebra Examples ===")
|
||||
|
||||
# Matrix creation and basic operations
|
||||
A = [1 2 3; 4 5 6; 7 8 10]
|
||||
b = [1, 2, 3]
|
||||
|
||||
println("Matrix A:")
|
||||
println(A)
|
||||
|
||||
# Solve linear system Ax = b
|
||||
x = A \ b
|
||||
println("\nSolution to Ax = b:")
|
||||
println(x)
|
||||
|
||||
# Eigenvalues
|
||||
eigenvals, eigenvecs = eigen(A)
|
||||
println("\nEigenvalues:")
|
||||
println(eigenvals)
|
||||
|
||||
# Singular value decomposition
|
||||
U, S, V = svd(A)
|
||||
println("\nSingular values:")
|
||||
println(S)
|
||||
|
||||
# Determinant and norm
|
||||
println("\nDeterminant: ", det(A))
|
||||
println("Frobenius norm: ", norm(A))
|
||||
end
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Numerical Integration Examples
|
||||
# ============================================================================
|
||||
|
||||
function integration_examples()
|
||||
using QuadGK
|
||||
|
||||
println("\n=== Numerical Integration Examples ===")
|
||||
|
||||
# Integrate sin(x) from 0 to π
|
||||
f1(x) = sin(x)
|
||||
result1, error1 = quadgk(f1, 0, π)
|
||||
println("∫sin(x)dx from 0 to π = ", result1)
|
||||
println("Estimated error: ", error1)
|
||||
|
||||
# Integrate exp(-x^2) from -∞ to ∞ (Gaussian)
|
||||
f2(x) = exp(-x^2)
|
||||
result2, error2 = quadgk(f2, -Inf, Inf)
|
||||
println("\n∫exp(-x²)dx from -∞ to ∞ = ", result2)
|
||||
println("Theoretical value: ", sqrt(π))
|
||||
|
||||
# Integrate 1/(1+x^2) from 0 to 1
|
||||
f3(x) = 1/(1 + x^2)
|
||||
result3, error3 = quadgk(f3, 0, 1)
|
||||
println("\n∫1/(1+x²)dx from 0 to 1 = ", result3)
|
||||
println("Theoretical value (π/4): ", π/4)
|
||||
end
|
||||
|
||||
# ============================================================================
|
||||
# Optimization Examples
|
||||
# ============================================================================
|
||||
|
||||
function optimization_examples()
|
||||
using Optim
|
||||
|
||||
println("\n=== Optimization Examples ===")
|
||||
|
||||
# Simple quadratic function
|
||||
f(x) = (x[1] - 2)^2 + (x[2] - 3)^2
|
||||
|
||||
result = optimize(f, [0.0, 0.0])
|
||||
println("Minimize f(x,y) = (x-2)² + (y-3)²")
|
||||
println("Minimum found at: ", Optim.minimizer(result))
|
||||
println("Minimum value: ", Optim.minimum(result))
|
||||
|
||||
# Rosenbrock function (more challenging)
|
||||
rosenbrock(x) = (1 - x[1])^2 + 100(x[2] - x[1]^2)^2
|
||||
|
||||
result2 = optimize(rosenbrock, [0.0, 0.0])
|
||||
println("\nMinimize Rosenbrock function")
|
||||
println("Minimum found at: ", Optim.minimizer(result2))
|
||||
println("Minimum value: ", Optim.minimum(result2))
|
||||
end
|
||||
|
||||
# ============================================================================
|
||||
# Statistics Examples
|
||||
# ============================================================================
|
||||
|
||||
function statistics_examples()
|
||||
using Statistics
|
||||
|
||||
println("\n=== Statistics Examples ===")
|
||||
|
||||
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20]
|
||||
|
||||
println("Data: ", data)
|
||||
println("\nStatistical measures:")
|
||||
println("Mean: ", mean(data))
|
||||
println("Median: ", median(data))
|
||||
println("Standard deviation: ", std(data))
|
||||
println("Variance: ", var(data))
|
||||
println("Minimum: ", minimum(data))
|
||||
println("Maximum: ", maximum(data))
|
||||
println("Range: ", maximum(data) - minimum(data))
|
||||
|
||||
# Quantiles
|
||||
println("\nQuantiles:")
|
||||
println("25th percentile: ", quantile(data, 0.25))
|
||||
println("50th percentile: ", quantile(data, 0.50))
|
||||
println("75th percentile: ", quantile(data, 0.75))
|
||||
end
|
||||
|
||||
# ============================================================================
|
||||
# Root Finding Examples
|
||||
# ============================================================================
|
||||
|
||||
function root_finding_examples()
|
||||
using Roots
|
||||
|
||||
println("\n=== Root Finding Examples ===")
|
||||
|
||||
# Find root of f(x) = x^3 - 2
|
||||
f(x) = x^3 - 2
|
||||
root = find_zero(f, 1.0)
|
||||
println("Root of x³ - 2 = 0: ", root)
|
||||
println("Verification: f(root) = ", f(root))
|
||||
|
||||
# Find root of f(x) = sin(x) - 0.5
|
||||
f2(x) = sin(x) - 0.5
|
||||
root2 = find_zero(f2, 0.5)
|
||||
println("\nRoot of sin(x) - 0.5 = 0: ", root2)
|
||||
println("Verification: f(root) = ", f2(root2))
|
||||
end
|
||||
|
||||
# ============================================================================
|
||||
# Main execution
|
||||
# ============================================================================
|
||||
|
||||
if abspath(PROGRAM_FILE) == @__FILE__
|
||||
linear_algebra_examples()
|
||||
integration_examples()
|
||||
optimization_examples()
|
||||
statistics_examples()
|
||||
root_finding_examples()
|
||||
end
|
||||
34
skills/julia-numerical/test_basic.jl
Normal file
34
skills/julia-numerical/test_basic.jl
Normal file
@@ -0,0 +1,34 @@
|
||||
# Basic Julia numerical test
|
||||
using LinearAlgebra
|
||||
using Statistics
|
||||
|
||||
println("Testing Julia Numerical Calculation Skill")
|
||||
println("==========================================\n")
|
||||
|
||||
# Test 1: Basic arithmetic
|
||||
println("Test 1: Basic Arithmetic")
|
||||
result = 2 + 2 * 3
|
||||
println("2 + 2 * 3 = ", result)
|
||||
|
||||
# Test 2: Vector operations
|
||||
println("\nTest 2: Vector Operations")
|
||||
v1 = [1, 2, 3]
|
||||
v2 = [4, 5, 6]
|
||||
dot_product = dot(v1, v2)
|
||||
println("dot([1,2,3], [4,5,6]) = ", dot_product)
|
||||
|
||||
# Test 3: Matrix operations
|
||||
println("\nTest 3: Matrix Operations")
|
||||
A = [1 2; 3 4]
|
||||
println("Matrix A:")
|
||||
println(A)
|
||||
println("det(A) = ", det(A))
|
||||
|
||||
# Test 4: Statistics
|
||||
println("\nTest 4: Statistics")
|
||||
data = [10, 20, 30, 40, 50]
|
||||
println("Data: ", data)
|
||||
println("mean = ", mean(data))
|
||||
println("std = ", std(data))
|
||||
|
||||
println("\n✓ All basic tests passed!")
|
||||
Reference in New Issue
Block a user