pts <- cbind(rnorm(400, sd = 3), rnorm(400))
# Find what triangle each point is in, and it triangular Barycentric
# coordinates
bary <- fm_bary(mesh2, loc = pts)
# How many points are outside the mesh?
sum(is.na(bary$t))
#> [1] 2
head(bary$bary)
#> [,1] [,2] [,3]
#> [1,] 0.04720217 0.5813611 0.3714367
#> [2,] 0.13930150 0.7219260 0.1387725
#> [3,] 0.43189448 0.1384910 0.4296145
#> [4,] 0.20127814 0.3716721 0.4270498
#> [5,] 0.21259133 0.3686084 0.4188003
#> [6,] 0.44390951 0.1739205 0.3821700
# Evaluate basis functions
basis <- fm_basis(mesh2, loc = pts) # Raw SparseMatrix
basis_object <- fm_basis(mesh2, loc = pts, full = TRUE) # fm_basis object
sum(!basis_object$ok)
#> [1] 2
# Construct an evaluator object
evaluator <- fm_evaluator(mesh2, loc = pts)
sum(!fm_basis(evaluator, full = TRUE)$ok)
#> [1] 2
# Values for the basis function weights; for ordinary 2d meshes this coincides
# with the resulting values at the vertices, but this is not true for e.g.
# 2nd order B-splines on 1d meshes.
field <- mesh2$loc[, 1]
value <- fm_evaluate(evaluator, field = field)
sum(abs(pts[, 1] - value), na.rm = TRUE)
#> [1] 5.438358e-14
pts1 <- seq(-2, 12, length.out = 1000)
# Find what segment, and its interval Barycentric coordinates
bary1 <- fm_bary(mesh1, loc = pts1)
# Points outside the interval are treated differently depending on the
# boundary conditions:
sum(is.na(bary1$t))
#> [1] 0
head(bary1$bary)
#> [,1] [,2]
#> [1,] 2.000000 -1.000000
#> [2,] 1.992993 -0.992993
#> [3,] 1.985986 -0.985986
#> [4,] 1.978979 -0.978979
#> [5,] 1.971972 -0.971972
#> [6,] 1.964965 -0.964965
# Evaluate basis functions
basis1 <- fm_basis(mesh1, loc = pts1) # Raw SparseMatrix
basis1_object <- fm_basis(mesh1, loc = pts1, full = TRUE) # fm_basis object
sum(!basis1_object$ok)
#> [1] 0
# Construct an evaluator object.
evaluator1 <- fm_evaluator(mesh1, loc = pts1)
# mesh_1d basis functions are defined everywhere
sum(!fm_basis(evaluator1, full = TRUE)$ok)
#> [1] 0
# Values for the basis function weights; for ordinary 2d meshes this coincides
# with the resulting values at the vertices, but this is not true for e.g.
# 2nd order B-splines on 1d meshes.
field1 <- rnorm(fm_dof(mesh1))
value1 <- fm_evaluate(evaluator1, field = field1)
plot(pts1, value1, type = "l")