conds='none' in Sympy does not work - sympy

I have the following integral
x,x1,x2,t=symbols('x x1 x2 t')
f=t*x1*x2*(x-t)**(-Rational('0.5'))
integrate(f,t).simplify()
The result of this is a piecewise function
Piecewise((2*sqrt(x)*x1*x2*(-I*t**2*sqrt((t - x)/x) - I*t*x*sqrt((t - x)/x) + 2*t*x + 2*I*x**2*sqrt((t - x)/x) - 2*x**2)/(3*(t - x)), Abs(t/x) > 1), (2*sqrt(x)*x1*x2*(-t**2*sqrt((-t + x)/x) - t*x*sqrt((-t + x)/x) + 2*t*x + 2*x**2*sqrt((-t + x)/x) - 2*x**2)/(3*(t - x)), True))
I want to ignore the first case, so the solution would be to do
integrate(f,t,conds='none').simplify()
But this doesn't change the output, which is still
Piecewise((2*sqrt(x)*x1*x2*(-I*t**2*sqrt((t - x)/x) - I*t*x*sqrt((t - x)/x) + 2*t*x + 2*I*x**2*sqrt((t - x)/x) - 2*x**2)/(3*(t - x)), Abs(t/x) > 1), (2*sqrt(x)*x1*x2*(-t**2*sqrt((-t + x)/x) - t*x*sqrt((-t + x)/x) + 2*t*x + 2*x**2*sqrt((-t + x)/x) - 2*x**2)/(3*(t - x)), True))
How can I ignore the conditions then?

I think the reason that conds does not affect this integral is that the conditions are not convergence conditions, but rather conditions that affect the form of the integrand. The documentation of conds specifically refers to conditions for convergence.
Definite improper integrals often entail delicate convergence conditions. Pass conds=’piecewise’, ‘separate’ or ‘none’ to have these returned, respectively, as a Piecewise function, as a separate result (i.e. result will be a tuple), or not at all (default is ‘piecewise’).
You can get rid of the case you don't want by replacing its condition with False.
>>> integrate(f, t).subs(Abs(t/x)>1, False).simplify()
2*sqrt(x)*x1*x2*(t**2*sqrt((-t + x)/x) + t*x*sqrt((-t + x)/x) - 2*t*x - 2*x**2*sqrt((-t + x)/x) + 2*x**2)/(3*(-t + x))

Related

Sympy solve cannot find real solution when solving first and substituting later

I have a system of two equations that calculate the coordinates of two points T1(u1,v1) and T2(u2,v2), where lines starting from point A to those points are tangent with a given circle. Point A is always outside of the circle, so there should always be two solutions.
The system of equations is given as follows:
eq1 = Eq((xpos-u)**2 + (ypos-v)**2, tglensq)
eq2 = Eq((oxpos-u)**2 + (oypos-v)**2, radsq)
where xpos,ypos are coordinates of A, and oxpos, oypos are coordinates of the center of the circle. tglensq represents the length of the tangent squared, and radsq the radius of the circle squared.
The solutions need to be calculated continuously in a loop.
I want to solve the equation first for u and v, and then plugin the other values (as described here). Solving for each iteration of the loop turns out to be quite expensive, although it always produces the two solutions.
After getting the solution, in the loop I substitute the other values, like so:
u1 = first_solution[0].subs([(xpos, robot_pos[0]), (ypos, robot_pos[1]), (oxpos, obstacle_pos[0]), (oypos, obstacle_pos[1]), (tglensq, tangent_len_squared), (minkradsq, minkowski_radius_sq)])
However, sympy fails to find the real solutions, and ends up giving imaginary ones, like:
628644.458063364 - 3693.82126269719*I
I saw here a similar issue about solving first then substituting, but I am not sure how to proceed from here. Any help would be greatly appreciated :).
Does defining the location of one circle relative to the other help? Let dx,dy = Point(oxpos,oypos)-Point(xpos,ypos) so the equations are
>>> dx,dy = symbols('dx,dy') # and your other symbols
>>> eq1,eq2 = Eq((u)**2 + (v)**2, tglensq),Eq((dx-u)**2 + (dy-v)**2, radsq)
>>> sol=solve((eq1,eq2), (u,v))
>>> sol[0]
(-(-dx**2 - dy**2 + 2*dy*(-dx*sqrt(-dx**4 - 2*dx**2*dy**2 + 2*dx**2*radsq + 2*dx**2*tglensq - dy**4 + 2*dy**2*radsq + 2*dy**2*tglensq - radsq**2 + 2*radsq*tglensq - tglensq**2)/(2*(dx**2 + dy**2)) + dy*(dx**2 + dy**2 - radsq + tglensq)/(2*(dx**2 + dy**2))) + radsq - tglensq)/(2*dx), -dx*sqrt(-dx**4 - 2*dx**2*dy**2 + 2*dx**2*radsq + 2*dx**2*tglensq - dy**4 + 2*dy**2*radsq + 2*dy**2*tglensq - radsq**2 + 2*radsq*tglensq - tglensq**2)/(2*(dx**2 + dy**2)) + dy*(dx**2 + dy**2 - radsq + tglensq)/(2*(dx**2 + dy**2)))
>>> sol[1]
(-(-dx**2 - dy**2 + 2*dy*(dx*sqrt(-dx**4 - 2*dx**2*dy**2 + 2*dx**2*radsq + 2*dx**2*tglensq - dy**4 + 2*dy**2*radsq + 2*dy**2*tglensq - radsq**2 + 2*radsq*tglensq - tglensq**2)/(2*(dx**2 + dy**2)) + dy*(dx**2 + dy**2 - radsq + tglensq)/(2*(dx**2 + dy**2))) + radsq - tglensq)/(2*dx), dx*sqrt(-dx**4 - 2*dx**2*dy**2 + 2*dx**2*radsq + 2*dx**2*tglensq - dy**4 + 2*dy**2*radsq + 2*dy**2*tglensq - radsq**2 + 2*radsq*tglensq - tglensq**2)/(2*(dx**2 + dy**2)) + dy*(dx**2 + dy**2 - radsq + tglensq)/(2*(dx**2 + dy**2)))
To calculate u,v try
>>> dif = (Point(oxpos,oypos)-Point(xpos,ypos)).subs(dict(zip((xpos,ypos,oxpos,oypos),(1,2,3,4))))
>>> Tuple(*sol).subs(dict(zip((tglensq,radsq),(5,6)))).subs(dict(zip((dx,dy),dif)))
Replace 1,2,3,4,5,6 with the actual values. Does that give you two real solutions?

Integrating a function with sympy - sympy.polys.polyerrors.PolynomialDivisionFailed error

I'm new to using Sympy, and wish to use it to integrate the following function. Although, I get this error which I'm not sure how to resolve.
import sympy as sy
A = sy.Symbol('A')
B = sy.Symbol('B')
C = sy.Symbol('C')
D = sy.Symbol('D')
x = sy.Symbol('x', positive=True)
y = sy.Symbol('y', positive=True)
def func(x,y):
return (1.0/(4.0*x*y))*(A*sy.log( ((y+x)**2 + C**2)/((y-x)**2 + C**2) ) - B*sy.log( ((y+x)**2 + D**2)/((y-x)**2 + D**2) ))
sy.integrate(func(x,y), (x,0,10))
Although, when I run this I get the following error and I'm not sure how to resolve this.
sympy.polys.polyerrors.PolynomialDivisionFailed: couldn't reduce degree in a polynomial division
algorithm when dividing [(-1.13305359211433e+221*y**2 + 5.66526796057163e+220*D**2 +
5.66526796057163e+220*C**2)/1.57368554460323e+219, 0.0, (3.39916077634298e+221*y**4 -
1.13305359211433e+221*y**2*D**2 - 1.13305359211433e+221*y**2*C**2 + 5.66526796057163e+220*D**4 +
2.26610718422865e+221*D**2*C**2 + 5.66526796057163e+220*C**4)/1.57368554460323e+219, 0.0,
(-3.39916077634298e+221*y**6 - 1.69958038817149e+221*y**4*D**2 - 1.69958038817149e+221*y**4*C**2
- 1.69958038817149e+221*y**2*D**4 + 6.79832155268595e+221*y**2*D**2*C**2 -
1.69958038817149e+221*y**2*C**4 + 1.69958038817149e+221*D**4*C**2 +
1.69958038817149e+221*D**2*C**4)/1.57368554460323e+219, 0.0, (1.13305359211433e+221*y**8 +
2.26610718422865e+221*y**6*D**2 + 2.26610718422865e+221*y**6*C**2 +
1.13305359211433e+221*y**4*D**4 + 4.5322143684573e+221*y**4*D**2*C**2 +
1.13305359211433e+221*y**4*C**4 + 2.26610718422865e+221*y**2*D**4*C**2 +
2.26610718422865e+221*y**2*D**2*C**4 + 1.13305359211433e+221*D**4*C**4)/1.57368554460323e+219,
0.0] by [(-2.0203139355353e+661*y**6 + 3.93961217429382e+662*y**4*D**2 +
3.93961217429382e+662*y**4*C**2 - 2.42437672264235e+662*y**2*D**4 -
3.03047090330294e+662*y**2*D**2*C**2 - 2.42437672264235e+662*y**2*C**4 +
2.52539241941912e+661*D**6 - 1.51523545165146e+661*D**4*C**2 - 1.51523545165146e+661*D**2*C**4 +
2.52539241941912e+661*C**6)/3.15674052427389e+659, 0.0, (6.06094180660588e+661*y**8 -
8.08125574214117e+662*y**6*D**2 - 8.08125574214117e+662*y**6*C**2 +
3.18199444846809e+662*y**4*D**4 + 2.47488457103073e+663*y**4*D**2*C**2 +
3.18199444846809e+662*y**4*C**4 - 1.51523545165147e+661*y**2*D**6 -
7.92973219697602e+662*y**2*D**4*C**2 - 7.92973219697602e+662*y**2*D**2*C**4 -
1.51523545165147e+661*y**2*C**6 + 2.52539241941911e+661*D**8 + 1.51523545165147e+661*D**6*C**2 -
2.02031393553529e+661*D**4*C**4 + 1.51523545165147e+661*D**2*C**6 +
2.52539241941911e+661*C**8)/3.15674052427389e+659, 0.0, (-6.06094180660588e+661*y**10 +
3.93961217429382e+662*y**8*D**2 + 3.93961217429382e+662*y**8*C**2 -
6.06094180660588e+661*y**6*D**4 + 9.6975068905694e+662*y**6*D**2*C**2 -
6.06094180660588e+661*y**6*C**4 + 1.38707976084194e+646*y**4*D**6 -
4.24265926462411e+662*y**4*D**4*C**2 - 4.24265926462411e+662*y**4*D**2*C**4 +
1.38707976084194e+646*y**4*C**6 - 3.03047090330294e+661*y**2*D**8 -
2.42437672264235e+662*y**2*D**6*C**2 - 2.42437672264235e+662*y**2*D**4*C**4 -
2.42437672264235e+662*y**2*D**2*C**6 - 3.03047090330294e+661*y**2*C**8 +
3.03047090330294e+661*D**8*C**2 + 1.73384970105243e+646*D**6*C**4 +
1.73384970105243e+646*D**4*C**6 + 3.03047090330294e+661*D**2*C**8)/3.15674052427389e+659, 0.0,
(2.02031393553529e+661*y**12 + 2.02031393553529e+661*y**10*D**2 +
2.02031393553529e+661*y**10*C**2 - 1.51523545165147e+661*y**8*D**4 +
1.01015696776765e+661*y**8*D**2*C**2 - 1.51523545165147e+661*y**8*C**4 -
1.01015696776765e+661*y**6*D**6 - 3.03047090330294e+661*y**6*D**4*C**2 -
3.03047090330294e+661*y**6*D**2*C**4 - 1.01015696776765e+661*y**6*C**6 +
5.05078483883823e+660*y**4*D**8 - 1.01015696776765e+661*y**4*D**6*C**2 -
1.01015696776765e+661*y**4*D**4*C**4 - 1.01015696776765e+661*y**4*D**2*C**6 +
5.05078483883823e+660*y**4*C**8 + 1.01015696776765e+661*y**2*D**8*C**2 +
1.01015696776765e+661*y**2*D**6*C**4 + 1.01015696776765e+661*y**2*D**4*C**6 +
1.01015696776765e+661*y**2*D**2*C**8 + 5.05078483883823e+660*D**8*C**4 +
1.01015696776765e+661*D**6*C**6 + 5.05078483883823e+660*D**4*C**8)/3.15674052427389e+659].
This can happen when it's not possible to detect zero in the coefficient domain. The domain of
computation is RR(B,y,D,C). Zero detection is guaranteed in this coefficient domain. This may
indicate a bug in SymPy or the domain is user defined and doesn't implement zero detection
properly.
Has anyone had this issue before and if so, how exactly can this be solved? I have varied the limits of $x$ but I get the same error!
Thank you in advance!

Derivative of a function with different formulas on different intervals

Is there a canonical way of declaring a function by parts in Sympy? I tried
import sympy
import sympy.functions.special.delta_functions as special
sympy.init_printing()
x = sympy.symbols('x', real=True)
V = x*x * (special.Heaviside(x + 1) - special.Heaviside(x - 1)) \
+ (1 + 2*sympy.log(x)) * special.Heaviside(x - 1) \
+ (1 + 2*sympy.log(-x)) * special.Heaviside(-x - 1)
which defines a differentiable function, but
print(V.diff(x).simplify())
# Prints: (x*(x**2*(-DiracDelta(x - 1) + DiracDelta(x + 1)) - 2*x*(Heaviside(x - 1) - Heaviside(x + 1)) - (2*log(-x) + 1)*DiracDelta(x + 1) + (2*log(x) + 1)*DiracDelta(x - 1)) + 2*Heaviside(-x - 1) + 2*Heaviside(x - 1))/x
Is there a way to somehow tell Sympy to simplify DiracDelta(x - a)*f(x) to DiracDelta(x - a)*f(a)?
Piecewise-defined functions are implemented by Piecewise class. Your function would be expressed as
V = sympy.Piecewise((1 + 2*sympy.log(-x), x < -1),
(x**2, x < 1),
(1 + 2*sympy.log(x), True))
print(V.diff(x))
which prints Piecewise((2/x, x < -1), (2*x, x < 1), (2/x, True))
The (expr, cond) pairs in Piecewise are processed in the order given: the first cond that evaluates to True (if the preceding evaluated to False) causes the corresponding expr to be returned.

Collecting the coefficients of a term in Sympy

I have the following expression in Sympy
s = e0*a01*d1**2*u0 - e0*a01*d1**2*u1 - e0*a11*d1**2*u0 - e0*a11*d1**2*u1 + e0*d0*a00*d1*u1 + e0*d0*a01*d1*u0 + e0*d0*a10*d1*u0 - e0*d0*a11*d1*u1 + e0*d0*b0*u0 - e0*d0*b1*u1 + e0*d1*a00*d1*u0 - e0*d1*a01*d1*u1 - e0*d1*a10*d1*u1 - e0*d1*a11*d1*u0 - e0*d1*b0*u1 - e0*d1*b1*u0 - e1*a00*d1**2*u0 + e1*a00*d1**2*u1 + e1*a10*d1**2*u0 + e1*a10*d1**2*u1 - e1*d0*a00*d1*u0 + e1*d0*a01*d1*u1 + e1*d0*a10*d1*u1 + e1*d0*a11*d1*u0 + e1*d0*b0*u1 + e1*d0*b1*u0 + e1*d1*a00*d1*u1 + e1*d1*a01*d1*u0 + e1*d1*a10*d1*u0 - e1*d1*a11*d1*u1 + e1*d1*b0*u0 - e1*d1*b1*u1
So first I simpify it:
s = sympify(s,locals=T)
(T contains all these symbols in the string, that are non commutative). And I want to get the coefficient of
d1**2*u0
after "factoring" it. So I did the following:
e=sympify(d1**2*u0,locals=T)
collected_expr = collect(s,e,exact=True)
print(collected_expr)
coeff = collected_expr.coeff(e)
print(coeff)
The result of collected_expr is ok:
d1**2*u0*(e0*a01 - e0*a11 - e1*a00 + e1*a10) - e0*a01*d1**2*u1 - e0*a11*d1**2*u1 + e0*d0*a00*d1*u1 + e0*d0*a01*d1*u0 + e0*d0*a10*d1*u0 - e0*d0*a11*d1*u1 + e0*d0*b0*u0 - e0*d0*b1*u1 + e0*d1*a00*d1*u0 - e0*d1*a01*d1*u1 - e0*d1*a10*d1*u1 - e0*d1*a11*d1*u0 - e0*d1*b0*u1 - e0*d1*b1*u0 + e1*a00*d1**2*u1 + e1*a10*d1**2*u1 - e1*d0*a00*d1*u0 + e1*d0*a01*d1*u1 + e1*d0*a10*d1*u1 + e1*d0*a11*d1*u0 + e1*d0*b0*u1 + e1*d0*b1*u0 + e1*d1*a00*d1*u1 + e1*d1*a01*d1*u0 + e1*d1*a10*d1*u0 - e1*d1*a11*d1*u1 + e1*d1*b0*u0 - e1*d1*b1*u1
But coeff is not ok, as it returns 1, but I really want
e0*a01 - e0*a11 - e1*a00 + e1*a10
EDIT: I also tried
coeff = collected_expr.coeff(u0).coeff(d1).coeff(d1)
and
coeff = collected_expr.coeff(u0).coeff(d1**2)
But both things returned 0
The docstring of Expr.coeff says
When x is noncommutative, the coefficient to the left (default) or
right of x can be returned. The keyword 'right' is ignored when
x is commutative.
collect does not seem to be noncommutative-aware, however, so the factors that were on the right may collect to the left.
>>> var("A B", commutative=False)
(A, B)
>>> collect(A*B+B*A**2,B)
B*(A + A**2)

Faster C# (or other .NET) Levenshtein distance implementation

Good night,
I have been working with fuzzy string matching for some time now, and using C with some pointers I could write a very fast (for my needs) implementation of the Levenshtein distance between two strings. I tried to port the code to C# using unsafe code and the fixed keyword, but the performance was way slower. So I chose to build a C++ dll and use [DllImport] from C#, automatically marshalling every string. The problem is that, after profiling, this keeps being the most time-consuming part of my program, taking between 50-57% of the total running time of the program. Since I think I will need to do some heavy work with lots of substrings of a text field coming from some 3-million database records, I think the time the Levenshtein distance is taking is almost unacceptable. That being, I would like to know if you have any suggestions, both algorithmic or programming-related, to the code below, or if you know of any better algorithm to calculate this distance?
#define Inicio1 (*(BufferVar))
#define Inicio2 (*(BufferVar+1))
#define Fim1 (*(BufferVar+2))
#define Fim2 (*(BufferVar+3))
#define IndLinha (*(BufferVar+4))
#define IndCol (*(BufferVar+5))
#define CompLinha (*(BufferVar+6))
#define TamTmp (*(BufferVar+7))
int __DistanciaEdicao (char * Termo1, char * Termo2, int TamTermo1, int TamTermo2, int * BufferTab, int * BufferVar)
{
*(BufferVar) = *(BufferVar + 1) = 0;
*(BufferVar + 2) = TamTermo1 - 1;
*(BufferVar + 3) = TamTermo2 - 1;
while ((Inicio1 <= *(BufferVar + 2)) && (Inicio2 <= *(BufferVar + 3)) && *(Termo1 + Inicio1) == *(Termo2 + Inicio2))
Inicio1 = ++Inicio2;
if (Inicio2 > Fim2) return (Fim1 - Inicio1 + 1);
while ((Fim1 >= 0) && (Fim2 >= 0) && *(Termo1 + Fim1) == *(Termo2 + Fim2))
{ Fim1--; Fim2--;}
if (Inicio2 > Fim2) return (Fim1 - Inicio1 + 1);
TamTermo1 = Fim1 - Inicio1 + 1;
TamTermo2 = Fim2 - Inicio2 + 1;
CompLinha = ((TamTermo1 > TamTermo2) ? TamTermo1 : TamTermo2) + 1;
for (IndLinha = 0; IndLinha <= TamTermo2; *(BufferTab + CompLinha * IndLinha) = IndLinha++);
for (IndCol = 0; IndCol <= TamTermo1; *(BufferTab + IndCol) = IndCol++);
for (IndCol = 1; IndCol <= TamTermo1; IndCol++)
for (IndLinha = 1; IndLinha <= TamTermo2; IndLinha++)
*(BufferTab + CompLinha * IndLinha + IndCol) = ((*(Termo1 + (IndCol + Inicio1 - 1)) == *(Termo2 + (IndLinha + Inicio2 - 1))) ? *(BufferTab + CompLinha * (IndLinha - 1) + (IndCol - 1)) : ((*(BufferTab + CompLinha * (IndLinha - 1) + IndCol) < *(BufferTab + CompLinha * (IndLinha - 1) + (IndCol - 1))) ? ((*(BufferTab + CompLinha * IndLinha + (IndCol - 1)) < *(BufferTab + CompLinha * (IndLinha - 1) + IndCol)) ? *(BufferTab + CompLinha * IndLinha + (IndCol - 1)) : *(BufferTab + CompLinha * (IndLinha - 1) + IndCol)) : ((*(BufferTab + CompLinha * IndLinha + (IndCol - 1)) < *(BufferTab + CompLinha * (IndLinha - 1) + (IndCol - 1))) ? *(BufferTab + CompLinha * IndLinha + (IndCol - 1)) : *(BufferTab + CompLinha * (IndLinha - 1) + (IndCol - 1)))) + 1);
return *(BufferTab + CompLinha * TamTermo2 + TamTermo1);
}
Please note that BufferVar and BufferTab are two external int * (in th case, int[] variables being marshalled from C#) which I do not instantiate in every function call to make the whole process faster. Still, this code is pretty slow for my needs. Can anyone please give me some suggestions, or, if possible, provide some better code?
Edit: The distance can't be bounded, I need the actual distance.
Thank you very much,
1. Brute Force
Here is an implementation of the Levenshtein Distance in Python.
def levenshtein_matrix(lhs, rhs):
def move(index): return (index+1)%2
m = len(lhs)
n = len(rhs)
states = [range(n+1), [0,]*(n+1)]
previous = 0
current = 1
for i in range(1, m+1):
states[current][0] = i
for j in range(1,n+1):
add = states[current][j-1] + 1
sub = states[previous][j] + 1
repl = states[previous][j-1] + abs(cmp(lhs[i-1], rhs[j-1]))
states[current][j] = min( repl, min(add,sub) )
previous = move(previous)
current = move(current)
return states[previous][n]
It's the typical dynamic programming algorithm, just taking advantage that since one only need the last row, keeping only two rows at a time is sufficient.
For a C++ implementation, you might look at LLVM's one (line 70-130), note the use of a stack allocated array of fixed size, replaced only when necessary by a dynamically allocated array.
I just can't follow up your code to try and diagnose it... so let's change the angle of attack. Instead of micro-optimizing the distance, we'll change the algorithm altogether.
2. Doing better: using a Dictionary
One of the issue you face is that you could do much better.
The first remark is that the distance is symmetric, though it doesn't change the overall complexity it will halve the time necessary.
The second is that since you actually have a dictionary of known words, you can build on that: "actor" and "actual" share a common prefix ("act") and thus you need not recompute the first stages.
This can be exploited using a Trie (or any other sorted structure) to store your words. Next you will take one word, and compute its distance relatively to all of the words stored in the dictionary, taking advantage of the prefixes.
Let's take an example dic = ["actor", "actual", "addict", "atchoum"] and we want to compute the distance for word = "atchoum" (we remove it from the dictionary at this point)
Initialize the matrix for the word "atchoum": matrix = [[0, 1, 2, 3, 4, 5, 6, 7]]
Pick the next word "actor"
Prefix = "a", matrix = [[0, 1, 2, 3, 4, 5, 6, 7], [1, 0, 1, 2, 3, 4, 5, 6]]
Prefix = "ac", matrix = [[0, 1, 2, 3, 4, 5, 6, 7], [1, 0, 1, 2, 3, 4, 5, 6], [2, 1, 1, 2, 3, 4, 5, 6]]
Prefix = "act", matrix = [[..], [..], [..], [..]]
Continue until "actor", you have your distance
Pick the next word "actual", rewind the matrix until the prefix is a prefix of our word, here up to "act"
Prefix = "actu", matrix = [[..], [..], [..], [..], [..]]
Continue until "actual"
Continue for the other words
What's important here is the rewind step, by preserving the computation done for the previous word, with which you share a good-length prefix, you effectively save a lot of work.
Note that this is trivially implemented with a simple stack and does not require any recursive call.
Try the simple approach first - don't use pointers and unsafe code - just code plain ordinary C#... but use the correct algorithm.
There is a simple and efficient algorithm on Wikipedia that uses dynamic programming and runs O(n*m) where n and m are the lengths of the inputs. I suggest you try implementing that algorithm first, as it is described there and only start optimizing it after you've implemented it, measured the performance and found it to be insufficient.
See also the section Possible improvements where it says:
By examining diagonals instead of rows, and by using lazy evaluation, we can find the Levenshtein distance in O(m (1 + d)) time (where d is the Levenshtein distance), which is much faster than the regular dynamic programming algorithm if the distance is small
If I had to guess where the problem is I'd probably start by looking at this line that runs inside two loops:
*(BufferTab + CompLinha * IndLinha + IndCol) = ((*(Termo1 + (IndCol + Inicio1 - 1)) == *(Termo2 + (IndLinha + Inicio2 - 1))) ? *(BufferTab + CompLinha * (IndLinha - 1) + (IndCol - 1)) : ((*(BufferTab + CompLinha * (IndLinha - 1) + IndCol) < *(BufferTab + CompLinha * (IndLinha - 1) + (IndCol - 1))) ? ((*(BufferTab + CompLinha * IndLinha + (IndCol - 1)) < *(BufferTab + CompLinha * (IndLinha - 1) + IndCol)) ? *(BufferTab + CompLinha * IndLinha + (IndCol - 1)) : *(BufferTab + CompLinha * (IndLinha - 1) + IndCol)) : ((*(BufferTab + CompLinha * IndLinha + (IndCol - 1)) < *(BufferTab + CompLinha * (IndLinha - 1) + (IndCol - 1))) ? *(BufferTab + CompLinha * IndLinha + (IndCol - 1)) : *(BufferTab + CompLinha * (IndLinha - 1) + (IndCol - 1)))) + 1);
There appears to be a lot of duplication there though it's hard for me to spot exactly what's going on. Could you factor some of that out? And you definitely need to make it more readable.
You shouldn't try all your possible words with the Levenshtein distance algortihm. You should use another faster metric to filter out the likely candidates and only on then use the Levenshtein to remove ambiguity. The first sieve can be based on a n-gram (trigram works often well) frequency histogram or a hash function.