Commit 5828eab3 authored by rnahme's avatar rnahme
Browse files

added Laplace matrix and corresponding test case

parent 6e8709fd
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 1 10:52:12 2016
@author: rebecca
This class is a collection of tests to check integrity of functions in the Utilities-module.
How to start:
open a terminal, go to the directory containing this file,
start ipython, then type 1. 'import Testing_Utilities' and 2. 'run Testing_Problems'
"""
import unittest
from proxtoolbox import Utilities
class Test_Utilities(unittest.TestCase):
def test_Laplacian_matrix(self):
Lap = Utilities.Laplace_matrix.Laplace_matrix(3)
self.assertTrue((Lap.toarray() == ([[3.,-1.,0.,-1.,0.,0.,0.,0.,0.],
[-1.,4.,-1.,0.,-1.,0.,0.,0.,0.],
[0.,-1.,3.,0.,0.,-1.,0.,0.,0.],
[-1.,0.,0.,3.,-1.,0.,-1.,0.,0.],
[0.,-1.,0.,-1.,4.,-1.,0.,-1.,0.],
[0.,0.,-1.,0.,-1.,3.,0.,0.,-1.],
[0.,0.,0.,-1.,0.,0.,3.,-1.,0.],
[0.,0.,0.,0.,-1.,0.,-1.,4.,-1.],
[0.,0.,0.,0.,0.,-1.,0.,-1.,3.]])).all())
if __name__ == "__main__":
unittest.main()
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 1 09:22:48 2016
@author: rebecca
"""
from numpy import array, concatenate, ones
from scipy.sparse import spdiags, eye, kron
def Laplace_matrix(n):
"""
Discrete Laplace operator with Neumann boundary conditions
This method calculates the Laplacian for a 2-dim. signal (usually an image) of size n x n.
The resulting matrix has size n*n x n*n.
:param: n (int)
:return: L (sparse array)
"""
dd = concatenate([-1*ones((1,n)), concatenate(([[3]], 4*ones((1,n-2)), [[3]]), axis=1), -1*ones((1,n))])
D = spdiags(dd, array([-1,0,1]), n,n)
E = eye(n)
ll = spdiags(ones((2,n*n)),array([-n,n]),n*n,n*n)
L = kron(E,D) - ll
return L
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment