Skip to content

Commit

Permalink
Merge branch 'main' of github.com:vlang/vsl into feature/new-lapack
Browse files Browse the repository at this point in the history
* 'main' of github.com:vlang/vsl:
  Replace panic with vsl_panic in graph.v (#214)
  Replace panic with vsl_panic in eval function (#212)
  change IImage.data from voidptr to &u8
  Add Planck Temperature to Constants (#210)
  Add partial derivatives and tests (#209)
  ci: comment out the whole super-linter job (too many false positives, it seems that the tool is not configured properly)
  ci: update Dockerfile to satisfy the lint job
  ci: change `master` to `main` in .github/workflows/lint.yml
  ci: upgrade to `super-linter/super-linter/[email protected]`
  fix `v check-md ~/.vmodules/vsl`
  fix compilation on macos with latest clang 15 and LAPACK from brew
  • Loading branch information
ulises-jeremias committed Aug 3, 2024
2 parents 3af89bf + e98c949 commit d9fd254
Show file tree
Hide file tree
Showing 50 changed files with 227 additions and 80 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
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
3 changes: 2 additions & 1 deletion examples/io_h5_relax/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Example - io_h5_relax 📘

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

## Instructions

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

This example demonstrates the usage of the V Scientific Library for demonstrating lazy generation using iterators.
This example demonstrates the usage of the V Scientific Library
for demonstrating lazy generation using iterators.

## Instructions

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

This example demonstrates the usage of the V Scientific Library for demonstrating linear algebra operations.
This example demonstrates the usage of the V Scientific Library
for demonstrating linear algebra operations.

## Instructions

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

This example demonstrates the usage of the V Scientific Library for demonstrating the K-means clustering algorithm.
This example demonstrates the usage of the V Scientific Library
for demonstrating the K-means clustering algorithm.

## Instructions

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

This example demonstrates the usage of the V Scientific Library for performing K-means clustering with plotting.
This example demonstrates the usage of the V Scientific Library
for performing K-means clustering with plotting.

## Instructions

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

This example demonstrates the usage of the V Scientific Library for performing K-Nearest Neighbors algorithm with plotting.
This example demonstrates the usage of the V Scientific Library
for performing K-Nearest Neighbors algorithm with plotting.

## Instructions

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

This example demonstrates the usage of the V Scientific Library for performing a basic linear regression.
This example demonstrates the usage of the V Scientific Library
for performing a basic linear regression.

## Instructions

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

This example demonstrates the usage of the V Scientific Library for performing an advanced linear regression.
This example demonstrates the usage of the V Scientific Library
for performing an advanced linear regression.

## Instructions

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

This example demonstrates the usage of the V Scientific Library for performing linear regression with plotting.
This example demonstrates the usage of the V Scientific Library
for performing linear regression with plotting.

## Instructions

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

This example demonstrates the usage of the V Scientific Library for performing sentiment analysis using machine learning.
This example demonstrates the usage of the V Scientific Library
for performing sentiment analysis using machine learning.

## Instructions

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

This example demonstrates the usage of the V Scientific Library for demonstrating basic MPI functionality.
This example demonstrates the usage of the V Scientific Library
for demonstrating basic MPI functionality.

## Instructions

Expand Down
Loading

0 comments on commit d9fd254

Please sign in to comment.