0

I am trying to take the inverse of a matrix in Python and keep getting a syntax error. I am new to Python. After doing an internet search and trying multiple things, I am still not getting it. Can someone take a look at my code and point me in the right direction? Error Message: python2.6 test.py File "test.py", line 39 inverse = mat1.I*mat2 ^ SyntaxError: invalid syntax

Thank you!

#import all of the needed libraries
import math
import matplotlib.pyplot as plt
import numpy
import array
import itertools
from numpy import linalg as LA

#variables and defs
x = []
y = []
h1 = 1
h2 = 5
h3 = 10
x1 = .5
x2 = 9.5
x3 = 4.5
y1 = .5
y2 = 2.5
y3 = 9.5


#create a 10x10 grid
for i in range(10):
    for j in range(10):
        x.append(i)
        y.append(j)
    j=0

#Triangle Interpolation Method 3
for i in range(100):
    xp = x(i)
    yp = y(i)

    mat1 = ([[(x1-x3),(x2-x3)],[(y1-y3), (y2-y3)]])  
    mat2 = ([(xp-x3), (yp-y3)]
    inverse = (LA.inv(mat1))*mat2

    w1 = inverse(1)
    w2 = inverse(2)
    w3 = 1-w1-w2

#check to see if the points fall within the triangle
if((w1 <=1 && w1 >=0) && (w2 <=1 && w2 >=0) && (w3 <=1 && w3>=0))
    z = (h1*w1)+(h2*w2)+(h3*w3)
.
.
.
user1118321
  • 25,567
  • 4
  • 55
  • 86
  • 1
    When looking for help with an Exception, like `SyntaxError`, it is generally best to post at least the last part of the trackback involved, as it reduces the time it takes for us to find the error. – Perkins Apr 03 '13 at 03:01

3 Answers3

3

In addition to the missing : pointed out by Nick Burns, Python doesn't use &&. You should use and instead:

if((w1 <=1 and w1 >=0) and (w2 <=1 and w2 >=0) and (w3 <=1 and w3>=0)):
    z = (h1*w1)+(h2*w2)+(h3*w3)

Further, Python permits the following syntax that simplifies your if condition a bit:

if (0 <= w1 <= 1) and (0 <= w2 <= 1) and (0 <= w3 <=1):
    z = (h1*w1)+(h2*w2)+(h3*w3)

edit:

And the actual error that's being indicated based on your comment is the unbalanced parentheses on this line:

mat2 = ([(xp-x3), (yp-y3)]

which should just be:

mat2 = [(xp-x3), (yp-y3)]

And which you could further write as just

mat2 = [xp-x3, yp-y3]

To make it easier to see the necessary delimiters match.

Ray
  • 4,531
  • 1
  • 23
  • 32
0

Your syntax error is more than likely coming from the if statement at the end of your code. You will get a syntaxError when an IF statement doesn't have a ':' at the end.

For example:

def hello(name):
    if name

SyntaxError: invalid syntax

Hope that helps!

Nick Burns
  • 973
  • 6
  • 4
  • It's not getting to the "if" statement. I actually have a colon but it somehow got deleted when I was formatting my code to post. The error I am receiving is as follows: File "test.py", line 39 inverse = (LA.inv(mat1))*mat2 ^ SyntaxError: invalid syntax – user2238565 Apr 03 '13 at 03:06
0

You're missing a closing paren.

mat2 = ([(xp-x3), (yp-y3)]

Should be

mat2 = ([(xp-x3), (yp-y3)])

You will get further syntax errors after fixing that, though. You can look at Ray and Nick Burns' answers for more.

Haz
  • 2,539
  • 1
  • 18
  • 20