Ask Your Question
0

How to multiply a list by an integer?

asked 2021-07-13 18:14:40 +0100

Jerry Caveney gravatar image

updated 2021-07-13 20:31:24 +0100

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!)

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2021-07-13 21:31:47 +0100

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]
edit flag offensive delete link more

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 ( 2021-07-13 21:47:56 +0100 )edit

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

FrédéricC gravatar imageFrédéricC ( 2021-07-14 08:19:33 +0100 )edit

thank you.

Jerry Caveney gravatar imageJerry Caveney ( 2021-07-14 17:36:14 +0100 )edit

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: 2021-07-13 18:14:40 +0100

Seen: 516 times

Last updated: Jul 13 '21