Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Tests

on:
push:
pull_request:
workflow_dispatch:

jobs:
cmake:
name: Build & Test
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]

steps:
- uses: actions/checkout@v7
with:
submodules: recursive

- name: Configure
run: cmake -S . -B build -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Release

- name: Build
run: cmake --build build --config Release

- name: Test
run: ctest --test-dir build --build-config Release --output-on-failure
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# default build directory
build/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "external/Catch2"]
path = external/Catch2
url = https://github.com/catchorg/Catch2.git
40 changes: 40 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
cmake_minimum_required(VERSION 3.16)

project(NDTable LANGUAGES C CXX)

function(enable_project_warnings target)
if(MSVC)
target_compile_options(${target} PRIVATE /W4)
else()
target_compile_options(${target} PRIVATE -Wall -Wextra -Wpedantic)
endif()
endfunction()

add_library(NDTable
src/Core.c
src/Interpolation.c
)

enable_project_warnings(NDTable)

target_include_directories(NDTable
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)

include(CTest)

if(BUILD_TESTING)
add_subdirectory(external/Catch2)

add_executable(NDTable_test
test/CoreTests.cpp
test/InterpolationTests.cpp
)

target_compile_features(NDTable_test PRIVATE cxx_std_14)
target_link_libraries(NDTable_test PRIVATE NDTable Catch2::Catch2WithMain)
enable_project_warnings(NDTable_test)

add_test(NAME NDTable_test COMMAND NDTable_test)
endif()
19 changes: 7 additions & 12 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
BSD 3-Clause License
BSD 2-Clause License

Copyright (c) 2017, Scientific Data Format
All rights reserved.
Copyright (c) 2026, Dassault Systemes

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
![Build Status](https://ci.appveyor.com/api/projects/status/github/ScientificDataFormat/NDTable?branch=master&svg=true)

# NDTable
A C library to inter- and extrapolate multi-dimensional data

NDTable is a C library to inter- and extrapolate multi-dimensional data.

## License

The code in this repository is licensed under the [3-clause BSD license](LICENSE)

----------------------------------------------

Copyright © 2017 Dassault Systèmes
1 change: 1 addition & 0 deletions external/Catch2
Submodule Catch2 added at 317ac1
156 changes: 156 additions & 0 deletions include/NDTable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#ifndef NDTABLE_H_
#define NDTABLE_H_

#ifdef __cplusplus
extern "C" {
#endif

/*! The maximum number of dimensions */
#define MAX_NDIMS 32

/*! Interpolation methods */
typedef enum {
NDTABLE_INTERP_HOLD = 1,
NDTABLE_INTERP_NEAREST,
NDTABLE_INTERP_LINEAR,
NDTABLE_INTERP_AKIMA,
NDTABLE_INTERP_FRITSCH_BUTLAND,
NDTABLE_INTERP_STEFFEN
} NDTable_InterpMethod_t;

/*! Extrapolation methods */
typedef enum {
NDTABLE_EXTRAP_HOLD = 1,
NDTABLE_EXTRAP_LINEAR,
NDTABLE_EXTRAP_NONE
} NDTable_ExtrapMethod_t;

/*! The structure that holds the data values */
typedef struct {
int ndims; //!< the number of dimensions of the table
int dims[MAX_NDIMS]; //!< extents of the dimensions
int numel; //!< the number of data values
int offs[MAX_NDIMS]; //!< the index offsets for the dimensions
double *data; //!< the data values
double *scales[MAX_NDIMS]; //!< array of pointers to the scale values
} NDTable_t;

typedef NDTable_t * NDTable_h;

/*! Interpolation status codes */
typedef enum {
NDTABLE_INTERPSTATUS_UNKNOWN_METHOD = -4,
NDTABLE_INTERPSTATUS_DATASETNOTFOUND = -3,
NDTABLE_INTERPSTATUS_WRONGNPARAMS = -2,
NDTABLE_INTERPSTATUS_OUTOFBOUNS = -1,
NDTABLE_INTERPSTATUS_OK = 0
} NDTable_InterpolationStatus;


/*! Get the last error message
*
* @return the error message
*/
const char * NDTable_get_error_message();

/*! Evaluate the value of the table at the given sample point using the specified inter- and extrapolation methods
*
* @param [in] table the table handle
* @param [in] nparams the number of dimensions
* @param [in] params the sample point
* @param [in] interp_method the interpolation method
* @param [in] extrap_method the extrapolation method
* @param [out] value the value at the sample point
*
* @return 0 if the value could be evaluated, -1 otherwise
*/
int NDTable_evaluate(NDTable_h table, int nparams, const double params[], NDTable_InterpMethod_t interp_method, NDTable_ExtrapMethod_t extrap_method, double *value);

/*! Evalute the total differential of the table at the given sample point and deltas using the specified inter- and extrapolation methods
*
* @param [in] table the table handle
* @param [in] nparams the number of dimensions
* @param [in] params the sample point
* @param [in] delta_params the the deltas
* @param [in] interp_method the interpolation method
* @param [in] extrap_method the extrapolation method
* @param [out] value the total differential at the sample point
*
* @return 0 if the value could be evaluated, -1 otherwise
*/
int NDTable_evaluate_derivative(NDTable_h table, int nparams, const double params[], const double delta_params[], NDTable_InterpMethod_t interp_method, NDTable_ExtrapMethod_t extrap_method, double *value);

/*! The maximum length of an error message */
#define MAX_MESSAGE_LENGTH 256

/*! Sets the error message */
void NDTable_set_error_message(const char *msg, ...);

/*! Allocates a new table
*
* @return a pointer to the new table
*/
NDTable_h NDTable_alloc_table();

/*! De-allocates a table
*
* @param [in] pointer to the table to de-allocate
*/
void NDTable_free_table(NDTable_h table);

/*! Converts index to subscripts
*
* @param [in] index the index to convert
* @param [in] table the table for which to convert the index
* @param [out] subs the subscripts
*/
void NDTable_ind2sub(const int index, const NDTable_h table, int *subs);

/*! Converts subscripts to index
*
* @param [in] subs the subscripts to convert
* @param [in] table the table for which to convert the subscripts
* @param [out] index the index
*/
void NDTable_sub2ind(const int *subs, const NDTable_h table, int *index);

double NDTable_get_value_subs(const NDTable_h table, const int subs[]);

/*! Helper function to the indices for the interpolation
*
* @param [in] value the value to search for
* @param [in] num_values the number of values
* @param [in] values the values
* @param [out] index the smallest index in [0;num_values-2] for which values[index] <= value
* @param [out] t the weight for the linear interpolation s.t. value == (1-t)*values[index] + t*values[index+1]
*
* @return 0
*/
void NDTable_find_index(double value, int num_values, const double values[], int *index, double *t, NDTable_ExtrapMethod_t extrap_method);

int NDTable_evaluate_internal(const NDTable_h table, const double *t, const int *subs, int *nsubs, int dim, NDTable_InterpMethod_t interp_method, NDTable_ExtrapMethod_t extrap_method, double *value, double *derivatives);

NDTable_h NDTable_create_table(int ndims, const int *dims, const double *data, const double **scales);

/*! Calculate the number of offsets from the dimensions
*
* @param [in] ndims the number of dimensions
* @param [in] dims the extent of the dimensions
* @param [out] offs array to write the offsets
*/
void NDTable_calculate_offsets(int ndims, const int dims[], int offs[]);

/*! Calculate the number of elements from the dimensions
*
* @param [in] ndims the number of dimensions
* @param [in] dims the extent of the dimensions
*
* @return the number of elements
*/
int NDTable_calculate_numel(int ndims, const int dims[]);

#ifdef __cplusplus
}
#endif

#endif /*NDTABLE_H_*/
Loading
Loading