def __mul__(self, other): from yanntricks.src.Constructors import phyFunction try: f = phyFunction(self.sage * other) except TypeError: f = phyFunction(self.sage * other.sage) return f
def visualParametricCurve(self, xunit, yunit): from yanntricks.src.Constructors import ParametricCurve from yanntricks.src.Constructors import phyFunction x = var('x') Vf1 = phyFunction(self.f1(xunit * x)) Vf2 = phyFunction(self.f2(yunit * x)) return ParametricCurve(Vf1, Vf2)
def parametric_curve(self): """ Return the parametric curve corresponding to `self`. The starting point is `self.I` and the parameters is the arc length. The parameter is positive on the side of `self.B` and negative on the opposite side. EXAMPLES:: sage: from yanntricks import * sage: segment=Segment(Point(0,0),Point(1,1)) sage: curve=segment.parametric_curve() sage: print curve(0) <Point(0,0)> sage: print curve(1) <Point(1/2*sqrt(2),1/2*sqrt(2))> sage: print curve(segment.length) <Point(1,1)> """ from yanntricks.src.Constructors import phyFunction from yanntricks.src.Constructors import ParametricCurve x = var('x') l = self.length f1 = phyFunction(self.I.x+x*(self.F.x-self.I.x)/l) f2 = phyFunction(self.I.y+x*(self.F.y-self.I.y)/l) return ParametricCurve(f1, f2, (0, l))
def phyFunction(self): if self.is_horizontal: # The trick to define a constant function is explained here: # http://groups.google.fr/group/sage-support/browse_thread/thread/e5e8775dd79459e8?hl=fr?hl=fr x = var('x') fi = SR(self.I.y).function(x) return phyFunction(fi) if not (self.is_vertical or self.is_horizontal): x = var('x') return phyFunction(self.slope*x+self.independent)
def getNextRegularFunctionParameters(self, lmin, lmax, fun, df, xunit=1, yunit=1): """ Return a value 'nl' of the parameter such that the integral of 'fun' from 'lmin' to 'nl' is 'df'. `lmax` - is the maximal value of the parameter. If the interval [lmin,lmax] reveals to be too small, return 'None' """ # Vcurve is the curve as visually seen taking the dilatation # into account. from yanntricks.src.Constructors import phyFunction from yanntricks.src.Constructors import ParametricCurve x = var('x') Vf1 = phyFunction(self.f1(xunit * x)) Vf2 = phyFunction(self.f2(yunit * x)) Vcurve = ParametricCurve(Vf1, Vf2) prop_precision = float(df) / 100 # precision of the interval if prop_precision == 0: raise ValueError("prop_precision is zero.") # We'll perform a dichotomy method. # 'too_large' is a value of the parameter we know to be too large # 'too_small' is a value of the parameter we know to be too small # 'ell' is the median value on which the condition is tested. # The dichotomy method consist to make 'too_large' or 'too_small' become 'ell' and to recalculate a new 'ell' too_small = lmin too_large = lmax if Vcurve.getFunctionIntegral(fun, too_small, too_large) < df: return None max_iter = 100 done_iter = 0 while done_iter < max_iter: ell = (too_large + too_small) / 2 done_iter += 1 integral = Vcurve.getFunctionIntegral(fun, lmin, ell) if abs(integral - df) < prop_precision: return ell if integral > df: too_large = ell if integral < df: too_small = ell raise ShouldNotHappenException("I give up with this dichotomy")
def __add__(self, other): from yanntricks.src.Constructors import phyFunction try: g = other.sage except AttributeError: g = other return phyFunction(self.sage + g)
def tangent_phyFunction(self, x0): """ Return the tangent at the given point as a :class:`phyFunction`. INPUT: - ``x0`` - a number OUTPUT: A :class:`phyFunction` that represents the tangent. This is an affine function. EXAMPLE:: sage: from yanntricks import * sage: g=phyFunction(cos(x)) sage: print g.tangent_phyFunction(pi/2) x |--> 1/2*pi - x sage: g.tangent_phyFunction(pi/2)(1) 1/2*pi - 1 """ from yanntricks.src.Constructors import phyFunction x = var('x') ca = self.derivative()(x0) h0 = self.get_point(x0).y return phyFunction(h0 + ca * (x - x0))
def derivative(self, n=1): """ return the derivative of the function. INPUT: - ``n`` - an interger (default = 1) the order of derivative. If n=0, return self. EXAMPLES:: sage: from yanntricks import * sage: f=phyFunction(x**2) sage: print f.derivative() x |--> 2*x sage: print f.derivative()(3) 6 sage: g(x)=cos(x) sage: print [g.derivative(i) for i in range(0,5)] [x |--> cos(x), x |--> -sin(x), x |--> -cos(x), x |--> sin(x), x |--> cos(x)] """ from yanntricks.src.Constructors import phyFunction x = var('x') if n == 0: try: return self.f except AttributeError: # Happens when self is a phyFunction instead of phyFunctionGraph return self if n == 1: if self._derivative == None: self._derivative = phyFunction(self.sage.derivative(x)) return self._derivative else: return self.derivative(n - 1).derivative()
def EnsurephyFunction(f): from yanntricks.src.Constructors import phyFunction try: k = phyFunction(f.sage) except AttributeError: pass try: k = f.phyFunction() except AttributeError: pass k = phyFunction(f) try: k.nul_function = f.nul_function except AttributeError: pass return k
def parametric_curve(self, a=None, b=None): """ Return the parametric curve associated to the circle. If optional arguments <a> and <b> are given, return the corresponding graph between the values a and b of the angle. The parameter of the curve is the angle in radian. """ from yanntricks.src.Constructors import ParametricCurve from yanntricks.src.Constructors import phyFunction from yanntricks.src.Exceptions import MissingPictureException if self._parametric_curve is None: x = var('x') if self.visual is True: if self.pspict is None: raise MissingPictureException( "You are trying to draw something with 'visual==True' when not giving a pspict." ) f1 = phyFunction(self.center.x + self.radius * cos(x) / self.pspict.xunit) f2 = phyFunction(self.center.y + self.radius * sin(x) / self.pspict.yunit) else: f1 = phyFunction(self.center.x + self.radius * cos(x)) f2 = phyFunction(self.center.y + self.radius * sin(x)) try: ai = self.angleI.radian af = self.angleF.radian except AttributeError: ai = self.angleI af = self.angleF self._parametric_curve = ParametricCurve(f1, f2, (ai, af)) curve = self._parametric_curve # The following is the typical line that is replaced by the decorator # 'copy_parameters' # curve.parameters=self.parameters.copy() if a == None: return curve else: return curve.graph(a, b)
def parametric_curve(self): """ return a parametric curve with the same graph as `self`. """ from yanntricks.src.Constructors import phyFunction from yanntricks.src.Constructors import ParametricCurve if self._parametric_curve: return self._parametric_curve x = var('x') curve = ParametricCurve(phyFunction(x), self, (self.mx, self.Mx)) curve.parameters = self.parameters.copy() curve.linear_plotpoints = self.linear_plotpoints curve.curvature_plotpoints = self.curvature_plotpoints curve.added_plotpoints = self.added_plotpoints curve._representativeParameters = self._representativeParameters self._parametric_curve = curve return curve
def __neg__(self): from yanntricks.src.Constructors import phyFunction return phyFunction(-self.sage).graph(self.mx, self.Mx)
def __pow__(self, n): from yanntricks.src.Constructors import phyFunction return phyFunction(self.sage**n)