-1

My aim is to take a triple (A, B, C), compute three "neighbours", and then output the maximum of each of those neighbours to a list.

For example, the neighbours of (sqrt(6), 4*sqrt(3), 9*sqrt(2)) are

(sqrt(3)*sqrt(2), 3*sqrt(2), 4*sqrt(3))

(4*sqrt(3), 35*sqrt(3)*sqrt(2), 9*sqrt(2))

(sqrt(3)*sqrt(2), 9*sqrt(2), 14*sqrt(3))

so the values 14*sqrt(3), 36*sqrt(6), 4*sqrt(3) would be the output.

When I try this:

A = 1*sqrt(6)
B = 4*sqrt(3)
C = 9*sqrt(2)


def nbhs_1(triple):
    X = triple[0]
    Y = triple[1]
    Z = triple[2]

    print((X.canonicalize_radical(), (X * Y - Z).canonicalize_radical(), Y.canonicalize_radical()))


def nbhs_2(triple):
    X = triple[0]
    Y = triple[1]
    Z = triple[2]  

    print((Y.canonicalize_radical(), (Y * Z - X).canonicalize_radical(), Z.canonicalize_radical()))


def nbhs_3(triple):
    X = triple[0]
    Y = triple[1]
    Z = triple[2]

    print((X.canonicalize_radical(), Z.canonicalize_radical(), (X * Z - Y).canonicalize_radical()))

result_1 = nbhs_1((A, B, C))
result_2 = nbhs_2((A, B, C))
result_3 = nbhs_3((A, B, C))

print(result_1)
print(result_2)
print(result_3)

l = [max(result_1), max(result_2), max(result_3)]

I get 'NoneType' object is not iterable.

ISD
  • 984
  • 1
  • 6
  • 21
user404920
  • 115
  • 3
  • You’re trying to get the max of a function without 1) sending any data into the function, and 2) returning anything from the functions. Address these two items and try again. – S3DEV Jul 24 '20 at 12:42
  • Please add more of your code. Where is `canonicalize_radical` coming from? – Jortega Jul 24 '20 at 12:48
  • Ok, I've amended the question, but I still get an error. Also, @Jortega, I'm using Sagemath (I'm sure this is all very elementary so I apologise, I started learning today!) – user404920 Jul 24 '20 at 12:51
  • all of your functions nbhs_1|2|3 return None, hence result_1|2|3 are all None. ```max``` expects something it can iterate over, e.g. a list. you cannot iterate over None... – mrxra Jul 24 '20 at 12:59
  • When I try running the code you posed I get `AttributeError: 'float' object has no attribute 'canonicalize_radical'`. I need more of your code like where you are importing Sagemath and how you are defining `sqrt`. – Jortega Jul 24 '20 at 13:00
  • @Jortega I'm running it on https://sagecell.sagemath.org/, so that's the entirety of the code – user404920 Jul 24 '20 at 13:02
  • @mrxra how would I amend the functions so that they return a tuple, and not None? – user404920 Jul 24 '20 at 13:03

2 Answers2

1

The problem is that you are not calling the functions nbhs_1, nbhs_2, and nbhs_3 and also the functions aren't returning any values

from math import sqrt

A=1*sqrt(6)
B=4*sqrt(3)
C=9*sqrt(2)
triple = (A, B, C)

def nbhs_1(triple):
    X=triple[0]
    Y=triple[1]
    Z=triple[2]
    return (X.canonicalize_radical(),(X*Y-Z).canonicalize_radical(),Y.canonicalize_radical())
def nbhs_2(triple):
    X=triple[0]
    Y=triple[1]
    Z=triple[2]  
    return (Y.canonicalize_radical(),(Y*Z-X).canonicalize_radical(),Z.canonicalize_radical())         
def nbhs_3(triple):
    X=triple[0]
    Y=triple[1]
    Z=triple[2]
    return (X.canonicalize_radical(),Z.canonicalize_radical(),(X*Z-Y).canonicalize_radical())

l=[max(nbhs_1(triple)),max(nbhs_2(triple)),max(nbhs_3(triple))]
chitrey
  • 11
  • 1
  • 1
1

The main problem is that you are not structuring the function properly:

  1. It is recommended that you expose your arguments within the function call. Don't def nbhs_1(triple), do instead def nbhs_1(X, Y, Z). In this way you can actually have one single function that does what you want (easier to maintain)
  2. Return your result. At the moment you are printing the outcome of the function call but you are not returning those results.
  3. I'm also not sure the canonicalize_radical() call is also done properly. Python is object-oriented and by writing var.canonicalize_radical() you are inferring that var should itself know about this function (e.g. the function is part of var) but that sounds wrong. The correct call may be canonicalize_radical(var)

Basically, this should be closer to a correct solution:

A=1*sqrt(6)
B=4*sqrt(3)
C=9*sqrt(2)

def nbhs(X, Y, Z):
    out1 = canonicalize_radical(X)
    out2 = canonicalize_radical(X*Y-Z)
    out3 = canonicalize_radical(Y)
    return out1, out2, out3

l = [max(nbhs(A, B, C)), max(nbhs(B, A, C)), max(nbhs(C, B, A))]
Alessio Arena
  • 390
  • 2
  • 8