Skip to content

Commit

Permalink
Merge pull request #167 from SimonRohou/codac2_dev
Browse files Browse the repository at this point in the history
Revised functions + trajectory operators
  • Loading branch information
SimonRohou authored Dec 27, 2024
2 parents 9882f6d + 60e0405 commit 7d5004a
Show file tree
Hide file tree
Showing 71 changed files with 1,853 additions and 1,270 deletions.
10 changes: 9 additions & 1 deletion doc/manual/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@ Codac manual

.. toctree::
:caption: User manual
:maxdepth: 3
:maxdepth: 2

installation/index.rst
intervals/index.rst
linear/index.rst
functions/index.rst
tubes/index.rst

.. toctree::
:maxdepth: 3

contractors/index.rst
separators/index.rst

.. toctree::
:maxdepth: 2

pavers/index.rst
cn/index.rst
geometry/index.rst
Expand Down
16 changes: 12 additions & 4 deletions examples/01_batman/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ int main()
VectorVar x(2);

AnalyticFunction f_half_wing {
{x}, vec(x[0],sqr(x[0]/7.)+sqr(x[1]/3.)) };
{x},
{ x[0],sqr(x[0]/7.)+sqr(x[1]/3.) }
};
AnalyticFunction f_half_head {
{x}, vec(0.75+3*x[0]-x[1]) };
{x},
{ 0.75+3*x[0]-x[1] }
};
AnalyticFunction f_half_neck {
{x}, vec(9-8*x[0]-x[1], (6.*sqrt(10.)/7.)+(1.5-0.5*x[0])-(6.*sqrt(10.)/14.)*sqrt(4-sqr(x[0]-1))-x[1]) };
{x},
{ 9-8*x[0]-x[1], (6.*sqrt(10.)/7.)+(1.5-0.5*x[0])-(6.*sqrt(10.)/14.)*sqrt(4-sqr(x[0]-1))-x[1] }
};
AnalyticFunction f_half_legs {
{x}, vec(((x[0]/2)-((3*sqrt(33)-7)/112)*sqr(x[0]))-3+sqrt(1-sqr(abs(x[0]-2)-1))-x[1]) };
{x},
{ ((x[0]/2)-((3*sqrt(33)-7)/112)*sqr(x[0]))-3+sqrt(1-sqr(abs(x[0]-2)-1))-x[1] }
};

SetFunction half_bat {
{ /* no arguments */ },
Expand Down
10 changes: 6 additions & 4 deletions examples/02_centered_form/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ using namespace codac2;
int main()
{
VectorVar x(3);
AnalyticFunction f({x}, vec(
-sqr(x[2])+2*x[2]*sin(x[2]*x[0])+cos(x[2]*x[1]),
2*x[2]*cos(x[2]*x[0])-sin(x[2]*x[1])
));
AnalyticFunction f { {x},
{
-sqr(x[2])+2*x[2]*sin(x[2]*x[0])+cos(x[2]*x[1]),
2*x[2]*cos(x[2]*x[0])-sin(x[2]*x[1])
}
};

CtcInverse_ ctc(f, IntervalVector::zero(2));
draw_while_paving({{0,2},{2,4},{0,10}}, ctc, 0.004);
Expand Down
4 changes: 2 additions & 2 deletions examples/02_centered_form/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from codac import *

x = VectorVar(3)
f = AnalyticFunction([x], vec(
f = AnalyticFunction([x], [
-sqr(x[2])+2*x[2]*sin(x[2]*x[0])+cos(x[2]*x[1]),
2*x[2]*cos(x[2]*x[0])-sin(x[2]*x[1])
))
])

ctc = CtcInverse(f, [0,0])
graphics.draw_while_paving([[0,2],[2,4],[0,10]], ctc, 0.004)
14 changes: 9 additions & 5 deletions examples/02_centered_form/main_evans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ using namespace codac2;
int main()
{
VectorVar x(4);
AnalyticFunction f({x}, vec(
sqr(x[0])-sqr(x[1])+2*exp(-x[0]*x[2])*(x[1]*sin(x[1]*x[2])+x[0]*cos(x[1]*x[2]))+exp(-x[0]*x[3])*cos(x[1]*x[3]),
2*x[0]*x[1]+2*exp(-x[0]*x[2])*(x[1]*cos(x[1]*x[2])-x[0]*sin(x[1]*x[2]))-exp(-x[0]*x[3])*sin(x[1]*x[3])
));
AnalyticFunction f { {x},
{
sqr(x[0])-sqr(x[1])+2*exp(-x[0]*x[2])*(x[1]*sin(x[1]*x[2])
+x[0]*cos(x[1]*x[2]))+exp(-x[0]*x[3])*cos(x[1]*x[3]),
2*x[0]*x[1]+2*exp(-x[0]*x[2])*(x[1]*cos(x[1]*x[2])
-x[0]*sin(x[1]*x[2]))-exp(-x[0]*x[3])*sin(x[1]*x[3])
}
};

CtcInverse_<IntervalVector> ctc(f, {0.,0.});
CtcInverse_ ctc(f, IntervalVector::zero(2));
IntervalVector x0({{-10,10},{0,20},{1,1},{2,2}});

shared_ptr<Figure2D> g = make_shared<Figure2D>("Evans", GraphicOutput::VIBES);
Expand Down
2 changes: 1 addition & 1 deletion examples/02_centered_form/main_parabolas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int main()
);

AnalyticFunction h({a}, fa(a[0],a[1])-fb(a[2],a[3]));
CtcInverse_<IntervalVector> ctc(h, {0.,0.,0.});
CtcInverse_ ctc(h, IntervalVector::zero(3));

IntervalVector x0({{0,1},{0,1},{0,1},{0,1}});
draw_while_paving(x0, ctc, 0.001);
Expand Down
40 changes: 40 additions & 0 deletions examples/04_explored_area/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# ==================================================================
# codac / basics example - cmake configuration file
# ==================================================================

cmake_minimum_required(VERSION 3.0.2)
project(codac_example LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Adding IBEX

# In case you installed IBEX in a local directory, you need
# to specify its path with the CMAKE_PREFIX_PATH option.
# set(CMAKE_PREFIX_PATH "~/ibex-lib/build_install")

find_package(IBEX REQUIRED)
ibex_init_common() # IBEX should have installed this function
message(STATUS "Found IBEX version ${IBEX_VERSION}")

# Adding Codac

# In case you installed Codac in a local directory, you need
# to specify its path with the CMAKE_PREFIX_PATH option.
# set(CMAKE_PREFIX_PATH "~/codac/build_install")

find_package(CODAC REQUIRED)
message(STATUS "Found Codac version ${CODAC_VERSION}")

# Compilation

if(FAST_RELEASE)
add_compile_definitions(FAST_RELEASE)
message(STATUS "You are running Codac in fast release mode. (option -DCMAKE_BUILD_TYPE=Release is required)")
endif()

add_executable(${PROJECT_NAME} main.cpp)
target_compile_options(${PROJECT_NAME} PUBLIC ${CODAC_CXX_FLAGS})
target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC ${CODAC_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PUBLIC ${CODAC_LIBRARIES} Ibex::ibex)
38 changes: 38 additions & 0 deletions examples/04_explored_area/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Example: A sampled trajectory, 'sampled_f' (composed of time-stamped positions with
// linear interpolation between them), is first obtained by discretizing an analytical
// expression (the function 'f,' which represents a Lissajous curve) and then appending
// an additional position. This trajectory is subsequently used in another analytical
// expression (function 'h'). The projection of an inverse separator is then employed
// to validate the result.

#include <codac>

using namespace codac2;

int main()
{
ScalarVar t;
AnalyticFunction f {
{t},
{ 2*cos(t), sin(2*t) }
};

Interval tdomain(0,5);
auto sampled_f = AnalyticTrajectory(f,tdomain).sampled(0.8);
sampled_f[6] = {0,-1};

VectorVar w(3);
auto g = sampled_f.as_function();
AnalyticFunction h {
{w},
sqr(w[0]-g(w[2])[0])+sqr(w[1]-g(w[2])[1])
};

SepInverse s_h(h, {0,0.1});
SepProj s_projh(s_h, {0,1}, {sampled_f.tdomain()});

DefaultView::set_window_properties({75,75},{700,700});
draw_while_paving({{-3,3},{-2,2}}, s_projh, 5e-2);
DefaultView::draw_trajectory(sampled_f);
DefaultView::draw_trajectory(AnalyticTrajectory(f,tdomain), Color::dark_gray());
}
33 changes: 33 additions & 0 deletions examples/04_explored_area/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from codac import *

# Example: A sampled trajectory, 'sampled_f' (composed of time-stamped positions with
# linear interpolation between them), is first obtained by discretizing an analytical
# expression (the function 'f,' which represents a Lissajous curve) and then appending
# an additional position. This trajectory is subsequently used in another analytical
# expression (function 'h'). The projection of an inverse separator is then employed
# to validate the result.

t = ScalarVar()
f = AnalyticFunction(
[t],
[ 2*cos(t), sin(2*t) ]
)

tdomain = [0,5]
sampled_f = AnalyticTrajectory(f,tdomain).sampled(0.8)
sampled_f[6] = [0,-1]

w = VectorVar(3)
g = sampled_f.as_function()
h = AnalyticFunction(
[w],
sqr(w[0]-g(w[2])[0])+sqr(w[1]-g(w[2])[1])
)

s_h = SepInverse(h, [0,0.1])
s_projh = SepProj(s_h, [0,1], [sampled_f.tdomain()])

DefaultView.set_window_properties([75,75],[700,700])
draw_while_paving([[-3,3],[-2,2]], s_projh, 5e-2)
DefaultView.draw_trajectory(sampled_f)
DefaultView.draw_trajectory(AnalyticTrajectory(f,tdomain), Color.dark_gray())
76 changes: 43 additions & 33 deletions python/codac/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,39 @@ def codac_error(message):

class AnalyticFunction:

def __init__(self, args, e):
if isinstance(e, (int,float,Interval,ScalarVar,ScalarExpr)):
self.f = AnalyticFunction_Scalar(args,e)
elif isinstance(e, (Vector,IntervalVector,VectorVar,VectorExpr)):
self.f = AnalyticFunction_Vector(args,e)
def __init__(self, args, e=None):
if e:
if isinstance(e, (int,float,Interval,ScalarVar,ScalarExpr)):
self.f = AnalyticFunction_Scalar(args,ScalarExpr(e))
elif isinstance(e, (Vector,IntervalVector,VectorVar,VectorExpr)):
self.f = AnalyticFunction_Vector(args,VectorExpr(e))
elif isinstance(e, list):
lst=[]
for e_i in e:
if isinstance(e_i, (int,float,Interval,ScalarVar,ScalarExpr)):
lst.append(ScalarExpr(e_i))
else:
codac_error("AnalyticFunction: invalid vectorial expression")
self.f = AnalyticFunction_Vector(args,lst)
else:
codac_error("AnalyticFunction: can only build functions from scalar or vector expressions")
else:
codac_error("AnalyticFunction: can only build functions from scalar or vector expressions")
if isinstance(args, (AnalyticFunction_Scalar,AnalyticFunction_Vector)):
self.f = args
else:
codac_error("AnalyticFunction: invalid function argument")

def input_size(self):
return self.f.input_size()

def eval(self,*args):
return self.f.eval(*args)
def real_eval(self,*args):
return self.f.real_eval(*args)

def natural_eval(self,*args):
return self.f.natural_eval(*args)
def eval(self,m,*args):
return self.f.eval(m,*args)

def centered_eval(self,*args):
return self.f.centered_eval(*args)
def eval(self,*args):
return self.f.eval(*args)

def diff(self,*args):
return self.f.diff(*args)
Expand All @@ -44,9 +58,9 @@ def __call__(self,*args):
lst=[]
for arg in args:
if isinstance(arg, (int,float,Interval,ScalarVar,ScalarExpr)):
lst.append(ScalarExpr(arg).raw_copy())
lst.append(ScalarExpr(arg))
elif isinstance(arg, (Vector,IntervalVector,VectorVar,VectorExpr)):
lst.append(VectorExpr(arg).raw_copy())
lst.append(VectorExpr(arg))
else:
codac_error("AnalyticFunction: invalid input arguments")
return self.f(lst)
Expand Down Expand Up @@ -119,24 +133,6 @@ def copy(self):
return self.c.copy()


class SepInverse(Sep):

def __init__(self, f, y, with_centered_form = True):
Sep.__init__(self, f.input_size())
if isinstance(f.f, AnalyticFunction_Scalar):
self.s = SepInverse_Interval(f.f,Interval(y),with_centered_form)
elif isinstance(f.f, AnalyticFunction_Vector):
self.s = SepInverse_IntervalVector(f.f,IntervalVector(y),with_centered_form)
else:
codac_error("SepInverse: can only build SepInverse from scalar or vector functions")

def separate(self,x):
return self.s.separate(x)

def copy(self):
return super().copy()


class Approx:

def __init__(self, x, eps = float_info.epsilon*10):
Expand Down Expand Up @@ -195,7 +191,7 @@ def cart_prod(*args):
mode = 1
lst.append(arg)

elif isinstance(arg, Sep):
elif isinstance(arg, (Sep,SepBase)):
if mode != -1 and mode != 2:
codac_error("cart_prod: invalid input arguments, was expecting a " + mode_str[mode] + ", got a separator")
mode = 2
Expand Down Expand Up @@ -271,6 +267,9 @@ def sampled(self, dt):
def primitive(self, y0, t):
return SampledTrajectory(self.traj.primitive(y0, t))

def as_function(self):
return AnalyticFunction(self.traj.as_function())

# Methods from AnalyticTrajectory:
# none

Expand All @@ -288,6 +287,14 @@ def __init__(self, m):
self.traj = SampledTrajectory_Vector(m)
else:
codac_error("SampledTrajectory: can only build this trajectory from maps of scalar or vector values")

# Methods from std::map:

def __setitem__(self, t, y):
self.traj[t] = y

def __getitem__(self, t):
return self.traj[t]

# Methods from TrajectoryBase:

Expand Down Expand Up @@ -318,6 +325,9 @@ def sampled(self, *args):
def primitive(self, y0, t):
return SampledTrajectory(self.traj.primitive(y0, t))

def as_function(self):
return AnalyticFunction(self.traj.as_function())

# Methods from SampledTrajectory:

def nb_samples(self):
Expand Down
2 changes: 1 addition & 1 deletion python/src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
functions/analytic/codac2_py_analytic_operations.cpp
functions/analytic/codac2_py_analytic_variables.cpp
functions/analytic/codac2_py_AnalyticFunction.h
functions/analytic/codac2_py_ExprWrapper.h
functions/analytic/codac2_py_AnalyticExprWrapper.h

geometry/codac2_py_Edge.cpp
geometry/codac2_py_geometry.cpp
Expand Down
Loading

0 comments on commit 7d5004a

Please sign in to comment.