Skip to content

Commit

Permalink
Added VectorSpace base interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-heltai committed Nov 25, 2014
1 parent 609848c commit 9a0818d
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 14 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,18 @@ in the `issues` section of the git-hub repository.
position of a **computable object**. [TBD: interface]

- An abstract python interface of type `VectorSpace`, used to describe
*one dimensional functions* on `[0,1]`, as *coefficients* times *basis
*one dimensional functions* on `[a,b]`, as *coefficients* times *basis
functions*, with access to single basis functions, their derivatives,
their support and the splitting of `[0,1]` into
their support and the splitting of `[a,b]` into
sub-intervals where *each basis* is assumed to be smooth. In
particular, the following structure should be implemented for each
type of basis:

- `VectorSpace.n_dofs`: the number of basis functions that span
the space (*degrees of freedom*)
- `VectorSpace.n_cells`: the number of sub-intervals of `[0,1]`
- `VectorSpace.n_dofs_per_end_point`: the number of dofs
associated with the end points
- `VectorSpace.n_cells`: the number of sub-intervals of `[a,b]`
where each basis is smooth
- `VectorSpace.cells`: the `n_cells+1` splitting points that make
the `cells` of the `VectorSpace`
Expand All @@ -153,12 +155,17 @@ in the `issues` section of the git-hub repository.
- The implementation of the above for

- Power basis functions
- Continuous Lagrange (Newton-Cotes with end points) basis
functions, of degree `n` with `N` repetitions
- Bernstein basis functions of degree `n` with `N` repetitions
- Continuous Lagrange (Newton-Cotes with and without end points)
basis functions, of degree `n` with `1` repetition
- Bernstein basis functions of degree `n` with `1` repetition
- B-spline basis functions with given degree and knot_vector
- NURBS basis functions with given degree and knot_vector

- A `CompositeVectorSpace`, taking a subdivision of the interval
`[a,b]`, a `VectorSpace` on `[0,1]` and constructing a composite
vector space which is a rescaled repetition of `VectorSpace` on each
subinterval

- An abstract python interface of type `TimeAnalysis`, capable of
taking derivatives and integrals of **collections of paths**, given
an actual `VectorSpace` object, and a matrix with
Expand Down
Empty file added interfaces/__init__.py
Empty file.
51 changes: 51 additions & 0 deletions interfaces/vector_space.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import numpy as np

class VectorSpace:
"""An abstract python interface used to describe *one dimensional
functions* on `[a,b]`, as *coefficients* times *basis functions*, with
access to single basis functions, their derivatives, their support and
the splitting of `[a,b]` into sub-intervals where *each basis* is
assumed to be smooth.
The base class constructs the constant vector space.
"""
def __init__(self, a=0.0, b=1.0):
""" Pure interface class. It generates the constant on [0,1]. """
self.n_dofs = 1
self.n_dofs_per_end_point = 0
self.n_cells = 1
self.cells = np.array([a, b])

def check_index(self, i):
assert i< self.n_dofs, \
'Trying to access base %, but we only have %'.format(i, self.n_dofs)

def basis(self, i):
self.check_index(i)
return lambda x: 1.0

def basis_der(self, i, d):
self.check_index(i)
return lambda x: 0.0

def basis_span(self, i):
self.check_index(i)
return (0, 1)

def cell_span(self, i):
self.check_index(i)
return [0]

def eval(self, c, x):
assert len(c) == self.n_dofs, \
'Incompatible vector. It should have length %. It has lenght %'.format(self.n_dofs, len(c))
# Find the cell where x lies:
y = 0*x
# TBD: make this function aware of locality
for i in xrange(self.n_dofs):
y += self.basis(i)(x)*c[i]

def element(self, c):
assert len(c) == self.n_dofs, \
'Incompatible vector. It should have length %. It has lenght %'.format(self.n_dofs, len(c))
return lambda x: self.eval(c, x)
8 changes: 0 additions & 8 deletions tests/sample_unit_test.py

This file was deleted.

19 changes: 19 additions & 0 deletions tests/test_vector_space.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from interfaces.vector_space import *

def test_vector_space_interface():
a = VectorSpace()
assert a.n_dofs == 1
assert a.n_cells == 1
try:
a.check_index(2)
assert False, 'Expecting Failure!'
except:
pass
assert a.basis(0)(.5) == 1

try:
a.basis(0)(1.2)
assert False, 'Expecting Failure!'
except:
pass

Empty file added utilities/__init__.py
Empty file.

0 comments on commit 9a0818d

Please sign in to comment.