Skip to content

Commit

Permalink
Merge branch 'main' into fix_poly
Browse files Browse the repository at this point in the history
  • Loading branch information
PottierLoic committed Sep 10, 2024
2 parents eae3bf6 + bdb5c39 commit 80d32ad
Show file tree
Hide file tree
Showing 76 changed files with 1,219 additions and 349 deletions.
50 changes: 25 additions & 25 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@ permissions:
contents: read

jobs:
super-linter:
name: Lint Code Base
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
actions: read
checks: read
statuses: write
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
# Full git history is needed to get a proper
# list of changed files within `super-linter`
fetch-depth: 0

- name: Lint Code Base
uses: super-linter/super-linter/slim@v6
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VALIDATE_ALL_CODEBASE: false
DEFAULT_BRANCH: master
LINTER_RULES_PATH: .
MARKDOWN_CONFIG_FILE: .markdownlint.json
## super-linter:
## name: Lint Code Base
## runs-on: ubuntu-latest
## permissions:
## contents: read
## pull-requests: write
## actions: read
## checks: read
## statuses: write
## steps:
## - name: Checkout Code
## uses: actions/checkout@v4
## with:
## # Full git history is needed to get a proper
## # list of changed files within `super-linter`
## fetch-depth: 0
##
## - name: Lint Code Base
## uses: super-linter/super-linter/slim@v6.7.0
## env:
## GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
## VALIDATE_ALL_CODEBASE: false
## DEFAULT_BRANCH: main
## LINTER_RULES_PATH: .
## MARKDOWN_CONFIG_FILE: .markdownlint.json

docs:
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ EOF

# install Docker tools (cli, buildx, compose)
COPY --from=gloursdocker/docker / /

USER ${USERNAME}
HEALTHCHECK CMD true

6 changes: 6 additions & 0 deletions consts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ consts.mksa_plancks_constant_hbar

Planck's constant divided by `2\pi`, `\hbar`.

```console
consts.mksa_planck_temperature
```

Planck temperature `T_p`.

```console
consts.num_avogadro
```
Expand Down
2 changes: 2 additions & 0 deletions consts/cgs.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub const cgs_plancks_constant_h = 6.62606896e-27 // g cm^2 / s

pub const cgs_plancks_constant_hbar = 1.05457162825e-27 // g cm^2 / s

pub const cgs_planck_temperature = 1.416785e+32 // Kelvin

pub const cgs_astronomical_unit = 1.49597870691e+13 // cm

pub const cgs_light_year = 9.46053620707e+17 // cm
Expand Down
2 changes: 2 additions & 0 deletions consts/cgsm.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub const cgsm_plancks_constant_h = 6.62606896e-27 // g cm^2 / s

pub const cgsm_plancks_constant_hbar = 1.05457162825e-27 // g cm^2 / s

pub const cgsm_planck_temperature = 1.416785e+32 // Kelvin

pub const cgsm_astronomical_unit = 1.49597870691e+13 // cm

pub const cgsm_light_year = 9.46053620707e+17 // cm
Expand Down
2 changes: 2 additions & 0 deletions consts/mks.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub const mks_plancks_constant_h = 6.62606896e-34 // kg m^2 / s

pub const mks_plancks_constant_hbar = 1.05457162825e-34 // kg m^2 / s

pub const mks_planck_temperature = 1.416785e+32 // Kelvin

pub const mks_astronomical_unit = 1.49597870691e+11 // m

pub const mks_light_year = 9.46053620707e+15 // m
Expand Down
2 changes: 2 additions & 0 deletions consts/mksa.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub const mksa_plancks_constant_h = 6.62606896e-34 // kg m^2 / s

pub const mksa_plancks_constant_hbar = 1.05457162825e-34 // kg m^2 / s

pub const mksa_planck_temperature = 1.416785e+32 // Kelvin

pub const mksa_astronomical_unit = 1.49597870691e+11 // m

pub const mksa_light_year = 9.46053620707e+15 // m
Expand Down
12 changes: 12 additions & 0 deletions deriv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ for values greater than `x`. 📉

This function is equivalent to calling `deriv.forward` with a negative step-size.

### `partial`

```v ignore
pub fn partial(f fn ([]f64) f64, x []f64, variable int, h f64) (f64, f64)
```

This function computes the partial derivative of the function `f` with respect to
a specified variable at point `x` using step-size `h`. It returns the derivative
in `result` and an error estimate in `abserr`. The function `f` should take an array
of coordinates and return a single value. This method provides both the derivative
and its error estimate.

## References and Further Reading

This work is a spiritual descendent of the Differentiation module in [GSL](https://github.com/ampl/gsl). 📖
Expand Down
30 changes: 30 additions & 0 deletions deriv/deriv.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module deriv

import vsl.func
import vsl.errors
import vsl.internal.prec
import math

Expand Down Expand Up @@ -113,3 +114,32 @@ pub fn forward(f func.Fn, x f64, h f64) (f64, f64) {
pub fn backward(f func.Fn, x f64, h f64) (f64, f64) {
return forward(f, x, -h)
}

/**
* partial is a function that computes the partial derivative of a multivariable function with respect to a specified variable.
*
* @param f The multivariable function for which the partial derivative is to be computed.
* @param x The point at which the partial derivative is to be computed, represented as an array of coordinates.
* @param variable The index of the variable with respect to which the partial derivative is to be computed.
* @param h The step size to be used in the central difference method.
*
* @return A tuple containing the value of the partial derivative and the estimated error.
*/
pub fn partial(f fn ([]f64) f64, x []f64, variable int, h f64) (f64, f64) {
if variable < 0 || variable >= x.len {
errors.vsl_panic('Invalid variable index', .efailed)
}

// Define a helper function that converts the multivariate function
// to a univariate function for the specified variable
partial_helper := func.Fn{
f: fn [f, x, variable] (t f64, _ []f64) f64 {
mut x_new := x.clone()
x_new[variable] = t
return f(x_new)
}
}

// Use the central difference method to compute the partial derivative
return central(partial_helper, x[variable], h)
}
24 changes: 24 additions & 0 deletions deriv/deriv_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ fn df6(x f64, _ []f64) f64 {
return -1.0 / (x * x)
}

fn f_multi(x []f64) f64 {
return x[0] * x[0] + x[1] * x[1] // f(x,y) = x^2 + y^2
}

fn df_multi_dx(x []f64) f64 {
return 2 * x[0] // ∂f/∂x = 2x
}

fn df_multi_dy(x []f64) f64 {
return 2 * x[1] // ∂f/∂y = 2y
}

fn test_deriv() {
f1_ := func.Fn.new(f: f1)
df1_ := func.Fn.new(f: df1)
Expand Down Expand Up @@ -100,6 +112,18 @@ fn test_deriv() {
assert deriv_test('central', f6_, df6_, 10.0)
assert deriv_test('forward', f6_, df6_, 10.0)
assert deriv_test('backward', f6_, df6_, 10.0)

// Partial derivative test
x := [2.0, 3.0]
h := 1e-5

// Partial derivative with respect to x
dx, _ := partial(f_multi, x, 0, h)
assert float64.tolerance(dx, df_multi_dx(x), 1e-5)

// Partial derivative with respect to y
dy, _ := partial(f_multi, x, 1, h)
assert float64.tolerance(dy, df_multi_dy(x), 1e-5)
}

fn deriv_test(deriv_method string, f func.Fn, df func.Fn, x f64) bool {
Expand Down
68 changes: 34 additions & 34 deletions errors/errno.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,73 @@ module errors

pub enum ErrorCode {
// success
success = 0
success = 0
// generic failure
failure = -1
failure = -1
// iteration has not converged
can_continue = -2
// input domain error, e.g sqrt(-1)
edom = 1
edom = 1
// output range error, e.g. exp(1e+100)
erange = 2
erange = 2
// invalid pointer
efault = 3
efault = 3
// invalid argument supplied by user
einval = 4
einval = 4
// generic failure
efailed = 5
efailed = 5
// factorization failed
efactor = 6
efactor = 6
// sanity check failed - shouldn't happen
esanity = 7
esanity = 7
// malloc failed
enomem = 8
enomem = 8
// problem with user-supplied function
ebadfunc = 9
ebadfunc = 9
// iterative process is out of control
erunaway = 10
erunaway = 10
// exceeded max number of iterations
emaxiter = 11
emaxiter = 11
// tried to divide by zero
ezerodiv = 12
ezerodiv = 12
// user specified an invalid tolerance
ebadtol = 13
ebadtol = 13
// failed to reach the specified tolerance
etol = 14
etol = 14
// underflow
eundrflw = 15
eundrflw = 15
// overflow
eovrflw = 16
eovrflw = 16
// loss of accuracy
eloss = 17
eloss = 17
// failed because of roundoff error
eround = 18
eround = 18
// matrix, vector lengths are not conformant
ebadlen = 19
ebadlen = 19
// matrix not square
enotsqr = 20
enotsqr = 20
// apparent singularity detected
esing = 21
esing = 21
// integral or series is divergent
ediverge = 22
ediverge = 22
// requested feature is not supported by the hardware
eunsup = 23
eunsup = 23
// requested feature not (yet) implemented
eunimpl = 24
eunimpl = 24
// cache limit exceeded
ecache = 25
ecache = 25
// table limit exceeded
etable = 26
etable = 26
// iteration is not making progress towards solution
enoprog = 27
enoprog = 27
// jacobian evaluations are not improving the solution
enoprogj = 28
enoprogj = 28
// cannot reach the specified tolerance in F
etolf = 29
etolf = 29
// cannot reach the specified tolerance in X
etolx = 30
etolx = 30
// cannot reach the specified tolerance in gradient
etolg = 31
etolg = 31
// end of file
eof = 32
eof = 32
}
15 changes: 11 additions & 4 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# V Scientific Library - Examples Directory 📚

This directory contains various examples demonstrating the capabilities and usage of the V Scientific Library. Each example showcases different functionalities, from machine learning algorithms to plotting and mathematical computations. Below is a summary of the examples included in this directory:
This directory contains various examples demonstrating the capabilities and usage
of the V Scientific Library. Each example showcases different functionalities,
from machine learning algorithms to plotting and mathematical computations.
Below is a summary of the examples included in this directory:

## Machine Learning Examples 🤖

Expand Down Expand Up @@ -91,7 +94,11 @@ This directory contains various examples demonstrating the capabilities and usag

### Important Information ⚠️

- Each example contains a `main.v` file with the V code demonstrating the specific functionality.
- Some examples include an additional `README.md` file. **You must read the `README.md` before running the example** to understand any prerequisites or specific instructions.
- Each example contains a `main.v` file with the V code demonstrating
the specific functionality.
- Some examples include an additional `README.md` file.
**You must read the `README.md` before running the example** to understand
any prerequisites or specific instructions.

To get started, navigate to the respective example directory and run the `main.v` file using the V compiler.
To get started, navigate to the respective example directory
and run the `main.v` file using the V compiler.
3 changes: 2 additions & 1 deletion examples/deriv_example/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Example - deriv_example 📘

This example demonstrates the usage of the V Scientific Library for demonstrating derivative calculation.
This example demonstrates the usage of the V Scientific Library
for demonstrating derivative calculation.

## Instructions

Expand Down
6 changes: 4 additions & 2 deletions examples/dist_histogram/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Example - dist_histogram 📘

This example demonstrates the usage of the V Scientific Library for creating a distribution histogram.
This example demonstrates the usage of the V Scientific Library
for creating a distribution histogram.

## Instructions

1. Ensure you have the V compiler installed. You can download it from [here](https://vlang.io).
2. Ensure you have the VSL installed. You can do it following the [installation guide](https://github.com/vlang/vsl?tab=readme-ov-file#-installation)!
2. Ensure you have the VSL installed. You can do it
following the [installation guide](https://github.com/vlang/vsl?tab=readme-ov-file#-installation)!
3. Navigate to this directory.
4. Run the example using the following command:

Expand Down
3 changes: 2 additions & 1 deletion examples/fft_plot_example/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Example - fft_plot_example 📘

This example demonstrates the usage of the V Scientific Library for demonstrating Fast Fourier Transform (FFT) with plotting.
This example demonstrates the usage of the V Scientific Library
for demonstrating Fast Fourier Transform (FFT) with plotting.

## Instructions

Expand Down
6 changes: 4 additions & 2 deletions examples/io_h5_dataset/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Example - io_h5_dataset 📘

This example demonstrates the usage of the V Scientific Library for demonstrating HDF5 I/O for datasets.
This example demonstrates the usage of the V Scientific Library
for demonstrating HDF5 I/O for datasets.

## Instructions

1. Ensure you have the V compiler installed. You can download it from [here](https://vlang.io).
2. Ensure you have the VSL installed. You can do it following the [installation guide](https://github.com/vlang/vsl?tab=readme-ov-file#-installation)!
2. Ensure you have the VSL installed. You can do it
following the [installation guide](https://github.com/vlang/vsl?tab=readme-ov-file#-installation)!
3. Navigate to this directory.
4. Run the example using the following command:

Expand Down
Loading

0 comments on commit 80d32ad

Please sign in to comment.