473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Coding problems

I got a list of numbers lets say A = [0.456, 0.4344, 1, -1, 4, 5, 6,8]

how can the function mentioned below be fixed in python using the list of numbers mentioned above and get the six different solutions of 'xf' for lets say the code mentioned below:
Expand|Select|Wrap|Line Numbers
  1. c=2
  2. xfmin = 0.432*c
  3. xfmax = 0.528*c
  4. def xf(a, b):                                           
  5.     return a+(b-a)*A
  6. xfrn= xf(xfmin, xfmax)
  7. print "xf=", xfrn
Apr 28 '10 #1

✓ answered by bvdet

Please use code tags when posting code. See posting guidelines here.

Add an argument to the function and iterate on the list in a for loop:
Expand|Select|Wrap|Line Numbers
  1. A = [0.456, 0.4344, 1, -1, 4, 5, 6,8]
  2.  
  3. def xf(a, b, item):
  4.     return a+(b-a)*item
  5.  
  6. c=2
  7. xfmin = 0.432*c
  8. xfmax = 0.528*c
  9.  
  10. for item in A:
  11.     xfrn= xf(xfmin, xfmax, item)
  12.     print "xf =", xfrn
BV - Moderator

8 1400
bvdet
2,851 Expert Mod 2GB
Please use code tags when posting code. See posting guidelines here.

Add an argument to the function and iterate on the list in a for loop:
Expand|Select|Wrap|Line Numbers
  1. A = [0.456, 0.4344, 1, -1, 4, 5, 6,8]
  2.  
  3. def xf(a, b, item):
  4.     return a+(b-a)*item
  5.  
  6. c=2
  7. xfmin = 0.432*c
  8. xfmax = 0.528*c
  9.  
  10. for item in A:
  11.     xfrn= xf(xfmin, xfmax, item)
  12.     print "xf =", xfrn
BV - Moderator
Apr 28 '10 #2
thank you very much for the reply,

since am a beginner in python I also wanted ask you another question:

let say if you got if you got a sequence of numbers lets say
Expand|Select|Wrap|Line Numbers
  1. A = [3.4, 5.6, 7.7, 9.7]
  2. B = [3.5, 4.7.-3.3, 8.7]
  3. C = [8.5, 6.7.-8.3, 4.7]
  4.  
  5. Z = A*B*C
the value of Z should have four answers


I tried using for loops, lets say for

Expand|Select|Wrap|Line Numbers
  1. A = [3.4, 5.6, 7.7, 9.7]
  2. B = [3.5, 4.7.-3.3, 8.7]
  3. C = [8.5, 6.7.-8.3, 4.7]
  4. for i in A:
  5. for j in B:
  6. for k in C:
  7. Z = i*j*k
print z # four possible answers

but it didn't do what I wanted it to do.
Could you please help me out solve this problem.
Apr 28 '10 #3
bvdet
2,851 Expert Mod 2GB
Since the lists are the same length (4) and you want four answers, one could assume you want to multiply the number at index i of list A by the corresponding index of the other two lists. Iterate on A using built-in function enumerate().
Expand|Select|Wrap|Line Numbers
  1. for i, item in enumerate(A):
  2.     print item*B[i]*C[i]
Apr 28 '10 #4
Is there any other way of doing this other than using enumerate

lets say if the scenario was such that

Expand|Select|Wrap|Line Numbers
  1.  A = [3.4, 5.6, 7.7, 9.7]
  2.  B = [3.5, 4.7.-3.3, 8.7]
  3.  C = [8.5, 6.7.-8.3, 4.7]
  4.  
  5. f=2
  6. g=4
  7.  
  8. z=A*B
  9. y=A*C*f
  10. u=C*g
  11.  
  12. ww=z*y*u
is there any possibility of obtaining four solutions for ww??

and also lets say if you had to use each one of the column of A B and C at each time, is it possible, cause I have tried different methods but am failing to get what am trying to achieve
Apr 29 '10 #5
Glenton
391 Expert 256MB
Even better is to use numpy arrays. Import numpy, convert a,b,c to numpy arrays, and then z=a*b*c does what you want it to do.
Apr 29 '10 #6
I tried using numpy arrays but i was getting this error

Traceback (most recent call last):

File "C:\Python25\GEM SA.py", line 74, in <module>
A = matrix([[a11,a12],[a21,a22]])
File "C:\Python25\Lib\site-packages\numpy\core\defmatrix.py", line 217, in __new__
arr = N.array(data, dtype=dtype, copy=copy)
ValueError: setting an array element with a sequence.

I would be really thankful if someone can help me out as am struggling to get the end result. I am trying to figure out how I can use each value from the each array to get an output result, in total i will get three different results, is there any method available to solve such a scenario

Expand|Select|Wrap|Line Numbers
  1. xfrn=array([0.937143,0.885173,1.0329])
  2.  
  3.  
  4. theta_freqrn= array([10.1579,9.99248,10.9348])
  5.  
  6. kappa_freqrn= array([4.95614, 4, 2])
  7.  
  8. arn = array([6.32918, 5, 2])
  9.  
  10. Mthetadotrn= array([-1.15940, 1, 4])
  11.  
  12.  
  13.  
  14. s = 7.5
  15. c = 2.0
  16. m = 100.0
  17.  
  18. rho = 1.225
  19. xcm = 0.5*c
  20.  
  21.  
  22. e = xfrn/c-0.25    
  23.  
  24.  
  25. vel   = zeros((3500,1),float)
  26. freq1 = zeros((3500,1),float)
  27. freq2 = zeros((3500,1),float)
  28. freq3 = zeros((3500,1),float)
  29. freq4 = zeros((3500,1),float)
  30. damp1 = zeros((3500,1),float)
  31. damp2 = zeros((3500,1),float)
  32. damp3 = zeros((3500,1),float)
  33. damp4 = zeros((3500,1),float)
  34.  
  35. vstart = 1.0
  36. vinc = 0.1
  37. for icount in range(3500):            
  38.     V=vstart + icount*vinc
  39.  
  40.     a11 = (m*s*s*s*c)/3.0
  41.     a12 = m*s*s*(c*c/2.0-c*xfrn)/2.0
  42.     a21 = a12
  43.     a22 = m*s*(c*c*c/3.0-c*c*xfrn+xfrn*xfrn*c)
  44.  
  45.     A = matrix([[a11,a12],[a21,a22]])
  46.     Ainv = linalg.inv(A)
  47.  
  48.     k1 = ((kappa_freqrn*arn)**2)*a11 
  49.     k2 = ((theta_freqrn*arn)**2)*a22  
  50.  
  51.     c11 = (rho*V*c*s*s*s*arn)/6.0
  52.     c12 = 0
  53.     c21 = -(rho*V*c*c*s*s*e*arn)/4.0
  54.     c22 = -(rho*V*c*c*c*s*Mthetadotrn)/8.0
  55.  
  56.     C = matrix([[c11,c12],[c21,c22]])
  57.  
  58.     AC = -Ainv*C 
  59.  
  60.     k11 = k1
  61.     k12 = (rho*V*V*c*s*s*arn)/4.0
  62.     k21 = 0
  63.     k22 = k2 - (rho*V*V*c*c*s*e*arn)/2.0
  64.     K = matrix([[k11,k12],[k21,k22]])
  65.  
  66.     AK = -Ainv*K
  67.  
  68.     matrix_bind_columns = concatenate((AK,AC),axis=1)
  69.     M = concatenate((matrix([[0.0,0.0,1.0,0.0],[0.0,0.0,0.0,1.0]]),matrix_bind_columns),axis=0)
  70.  
  71.     Meig = linalg.eig(M)[0]
  72.  
  73.     vel[icount] = V
  74.     freq1[icount] = abs(Meig[0])
  75.     damp1[icount] = -100.0*real(Meig[0])/freq1[icount]
  76.     freq1[icount] = freq1[icount]/2*math.pi
  77.     freq2[icount] = abs(Meig[1])
  78.     damp2[icount] = -100.0*real(Meig[1])/freq2[icount]
  79.     freq2[icount] = freq2[icount]/2*math.pi
  80.     freq3[icount] = abs(Meig[2])
  81.     damp3[icount] = -100.0*real(Meig[2])/freq3[icount]
  82.     freq3[icount] = freq3[icount]/2*math.pi
  83.     freq4[icount] = abs(Meig[3])
  84.     damp4[icount] = -100.0*real(Meig[3])/freq4[icount]
  85.     freq4[icount] = freq4[icount]/2*math.pi
  86.  
  87.     if damp3[icount] < 0:           goes below zero
  88.         break
  89.  
  90. ab = (damp3[icount-1])
  91.  
  92.  
  93. bc = (damp3[icount])
  94.  
  95.  
  96. Vf_min = V-0.1
  97.  
  98.  
  99. Vf_max = V
  100.  
  101.  
  102. def flutter_speed(ab,bc,Vf_min,Vf_max):                  
  103.     return (Vf_min+(Vf_max - Vf_min)*(-(ab)/(bc - ab)))
  104. Vf = flutter_speed(ab,bc,Vf_min,Vf_max)
  105. print Vf
Apr 29 '10 #7
I am wondering if anyone can help me fix the array problem out?? If someone can give me feedback on where I am going wrong??
Apr 29 '10 #8
Glenton
391 Expert 256MB
Do you mean just for the multiplication?

Expand|Select|Wrap|Line Numbers
  1. In [1]: from numpy import *
  2.  
  3. In [2]: A=array([3.4,5.6,7.7,9.7])
  4.  
  5. In [3]: B=array([3.5,4.7,-3.3,8.7])
  6.  
  7. In [4]: C=array([8.5,6.7,-8.3,4.7])
  8.  
  9. In [5]: f=2
  10.  
  11. In [6]: g=4
  12.  
  13. In [7]: z=A*B
  14.  
  15. In [8]: y=A*C*f
  16.  
  17. In [9]: u=C*g
  18.  
  19. In [10]: ww=z*y*u
  20.  
  21. In [11]: z
  22. Out[11]: array([ 11.9 ,  26.32, -25.41,  84.39])
  23.  
  24. In [12]: y
  25. Out[12]: array([  57.8 ,   75.04, -127.82,   91.18])
  26.  
  27. In [13]: u
  28. Out[13]: array([ 34. ,  26.8, -33.2,  18.8])
  29.  
  30. In [14]: ww
  31. Out[14]: array([  23385.88   ,   52931.41504, -107830.48584,  144659.98776])
  32.  
  33.  
If it's something else, maybe start a new post.
Apr 29 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Mikkel christensen | last post by:
Hi there I wonder if any of you could point me in a direction where I can find some usefull information about coding standarts. I have some generel experiense in programming but I lack many...
0
by: Berthold Höllmann | last post by:
I have a default coding header # -*- coding: iso-8859-15 -*- in my python files. I now have Problems with this settings. I swithched to Python 2.4.1 under Windows. When I import files with the...
2
by: manning_news | last post by:
Has anyone had a problem with Access 2003 not saving their coding? I've been using 2003 for a couple of months now and just this week noticed that some coding I'd done for a database was not there...
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
3
by: | last post by:
I was wondering, I don't mean to be rude at all to ask this but i'd like to know if any of you guys have had problems in relating to getting paid a fair amount when it comes to coding. ...
23
by: Simon Hengel | last post by:
Hello, we are hosting a python coding contest an we even managed to provide a price for the winner... http://pycontest.net/ The contest is coincidentally held during the 22c3 and we will be...
78
by: Robert Baer | last post by:
The homepage i have had up and seemingly working is: http://oil4lessllc.com/ However, the validator has so many complaints, and being so incompetent, i have no clue as to how to fix it all. Would...
3
by: juliehg | last post by:
Hi, I'm currently using MS Access 2000 to keep all my actors measurements, resumes and personal details on a database. To upload their details onto our website, I have built an event so when I...
0
by: pat | last post by:
CodeCheck Coding Standard's Support As a free service to our customers we offer support in developing "rule-files" for automating corporate coding standards. If you have a coding standard that...
23
by: LayneMitch via WebmasterKB.com | last post by:
Hello. It's me again with another question that should generate a lot of responses. As mentioned before, I'm working on my first site (hopefully I'll be done soon and can move on to other...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.