All problems
Finite Difference Laplacian Build
Problem
Build the 1D finite difference Laplacian matrix of size n:
diagonal entries are 2, the entries just above and below the diagonal are -1, and all others are 0.
Hint
This matrix discretizes the second derivative on a 1D grid.
Test cases
case 1
in: [1]
expected: [[2]]
case 2
in: [2]
expected: [[2,-1],[-1,2]]
case 3
in: [3]
expected: [[2,-1,0],[-1,2,-1],[0,-1,2]]
case 4
in: [4]
expected: [[2,-1,0,0],[-1,2,-1,0],[0,-1,2,-1],[0,0,-1,2]]
Starter codePython
def laplacian_1d(n):
# Your code here
pass4 test cases · run your code against them in the editor