Skip to content

Instantly share code, notes, and snippets.

@Evgenij-Gr
Created January 3, 2015 09:19
Show Gist options
  • Save Evgenij-Gr/b6a9190925f1d13ba86d to your computer and use it in GitHub Desktop.
Save Evgenij-Gr/b6a9190925f1d13ba86d to your computer and use it in GitHub Desktop.
Simple check for transversality of vector field along straight line or curve. Check is based on mean value theorem: if there are points where scalar product of vector field with normal field have different signs, then exists a point of non-transversality.
import numpy as np
import math as m
# we assume that line is parameterized by values from segment [0, 1] placed uniformly
# startPoint, endPoint - 2D point
# nSteps - integer value
# vectorField - function that takes 2D point and return 2D vector
def checkStraightLineForTransversality(startPoint, endPoint, nSteps, vectorField):
tangentVector = endPoint - startPoint
normalVector = np.array([-tangentVector[1], tangentVector[0]])
# normalize it!
normalVector = normalVector/m.sqrt(np.dot(normalVector, normalVector))
normVectors = [normalVector for n in xrange(nSteps)]
innerSegmentPoints = np.linspace(0.0,1.0,nSteps)
vectorFieldAtInnerPoints = [vectorField(startPoint + t*(endPoint - startPoint)) for t in innerSegmentPoints]
# normalize them all!
vectorFieldAtInnerPoints = [v/m.sqrt(np.dot(v,v)) for v in vectorFieldAtInnerPoints]
# check crossing direction (outwards/inwards)
crossingDirections = [np.dot(normal, vField) for (normal, vField) in zip(normVectors, vectorFieldAtInnerPoints)]
minDP = min(crossingDirections)
maxDP = max(crossingDirections)
if (minDP * maxDP > 0):
return {"normal": normalVector,
"points": innerSegmentPoints,
"dotProductValue": crossingDirections,
"result": "Transversal"}
else:
return {"normal": normalVector,
"points": innerSegmentPoints,
"dotProductValue": crossingDirections,
"result": "Non-transversal"}
def checkCurveForTransversality(vectorField, tangentField, curveParameterization, nSteps=50, parameterizationPoints=[]):
if (parameterizationPoints==[]):
parameterizationPoints = np.linspace(0.0, 1.0, nSteps, endPoint=True)
points = [curveParameterization(pP) for pP in parameterizationPoints]
tangentVectors = [tangentField(p) for p in points]
normalVectors = [[-v[1], v[0]] for v in tangentVectors]
normalVectors = [n/m.sqrt(np.dot(n,n)) for n in normalVectors]
vectorField = [vectorField(p) for p in points]
vectorField = [v/m.sqrt(np.dot(v,v)) for v in vectorField]
crossingDirection = [np.dot(normal, vfield) for (normal, vfield) in zip(normalVectors, vectorField)]
minDP = min(crossingDirection)
maxDP = max(crossingDirection)
if (minDP * maxDP > 0):
return {"normal": normalVectors,
"points": points,
"dotProductValue": crossingDirection,
"result": "Transversal"}
else:
return {"normal": normalVectors,
"points": points,
"dotProductValue": crossingDirection,
"result": "Non-transversal"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment