Ask Your Question
0

How to multiply a list by an integer?

asked 3 years ago

Jerry Caveney gravatar image

updated 3 years ago

This is so basic I'm embarrassed to even ask the question, but I've tried all the possibilities I can think of, and searched two SM guides (SM Tutorial and SM for Undergrads), and I can't find out how to do this. I want to multiply a list of rational numbers (fractions) by an integer. I've tried with lists and sets (although sets wouldn't really work, because the list of fractions contains repetitions), and I've tried with the single integer as simply a number and as a list. But I always get an error message, saying "unsupported operand type(s)" or something similar. Here is an example of what I'm trying to do:

a = [1, 1/2, 1/3, 3/8, 1/5, 1/3, 3/14, 1/4, 1/6, 1/4,]

b = 840

print (a ^ b)

Help for a clueless newbie would be greatly appreciated. (Also, if anyone can recommend a tutorial or guide that will explain how to do these sorts of things in SM, that would be even better!)

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 3 years ago

FrédéricC gravatar image

like this

sage: a = [1, 1/2, 1/3, 3/8, 1/5, 1/3, 3/14, 1/4, 1/6, 1/4]                     
sage: b = 840                                                                   
sage: b * vector(a)                                                             
(840, 420, 280, 315, 168, 280, 180, 210, 140, 210)
sage: [b * n for n in a]                                                        
[840, 420, 280, 315, 168, 280, 180, 210, 140, 210]
Preview: (hide)
link

Comments

Thank you! Do you know of a site where I can learn why this works, and how similar problems have to be programmed to work?

Jerry Caveney gravatar imageJerry Caveney ( 3 years ago )

You may want to learn Python first. There are many books abut Python.

FrédéricC gravatar imageFrédéricC ( 3 years ago )

thank you.

Jerry Caveney gravatar imageJerry Caveney ( 3 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 3 years ago

Seen: 722 times

Last updated: Jul 13 '21