Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
ogs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Özgür Ozan Sen
ogs
Commits
cce15ade
Commit
cce15ade
authored
11 years ago
by
Norihiro Watanabe
Browse files
Options
Downloads
Patches
Plain Diff
add LinearFunction in MathLib
parent
1744a90a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
MathLib/LinearFunction.h
+74
-0
74 additions, 0 deletions
MathLib/LinearFunction.h
with
74 additions
and
0 deletions
MathLib/LinearFunction.h
0 → 100644
+
74
−
0
View file @
cce15ade
/**
* \author Norihiro Watanabe
* \date 2013-08-13
*
* \copyright
* Copyright (c) 2013, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
#ifndef LINEARFUNCTION_H_
#define LINEARFUNCTION_H_
#include
<array>
#include
<algorithm>
#include
<cassert>
namespace
MathLib
{
/**
* Linear function
* \f[
* f(x_1,...,x_k)=a_0+a_1*x_1+...+a_k*x_k
* \f]
*
* \tparam T_TYPE value type
* \tparam N_VARS the number of variables (k)
*/
template
<
typename
T_TYPE
,
unsigned
N_VARS
>
class
LinearFunction
{
public:
/**
* Constructor
* \param coefficients an array of coefficients of a linear function.
* The size of the coefficient array should equal to the number of variables + 1
*/
explicit
LinearFunction
(
const
T_TYPE
*
coefficients
)
:
_coefficients
(
copy
(
coefficients
))
{
static_assert
(
N_VARS
>
0
,
"Template parameter N_VARS in LinearFunction should be nonzero"
);
}
/**
* evaluate the function
* \param x an array of variables. the size of the array should equal to the number of variables + 1
*/
T_TYPE
operator
()(
T_TYPE
const
*
const
x
)
const
{
T_TYPE
v
=
_coefficients
[
0
];
for
(
unsigned
i
=
0
;
i
<
N_VARS
;
i
++
)
v
+=
_coefficients
[
i
+
1
]
*
x
[
i
];
return
v
;
}
private
:
/// copy a given raw array to std::array
static
std
::
array
<
T_TYPE
,
N_VARS
+
1
>
copy
(
const
T_TYPE
*
coefficients
)
{
std
::
array
<
T_TYPE
,
N_VARS
+
1
>
temp
;
std
::
copy
(
coefficients
,
coefficients
+
temp
.
size
(),
temp
.
data
());
return
temp
;
}
/// Coefficients of a linear function
const
std
::
array
<
T_TYPE
,
N_VARS
+
1
>
_coefficients
;
};
}
// MathLib
#endif
/* LINEARFUNCTION_H_ */
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment