Advertisement
Hazem09

Revision

Jun 14th, 2022
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. # CVX
  2. import cvxpy as cp
  3. import numpy as np
  4. A=np.array([[1,1,1],[1,2,3]])
  5. b=np.array([6,5])
  6. c=np.array([-1,-2,-4])
  7. n=3
  8. x=cp.Variable(n,integer=True)
  9. P=cp.Problem(cp.Minimize(c@x),[A@x<=b,x>=0])
  10. P.solve(cp.ECOS_BB)
  11. print(x.value)
  12. print(P.value)
  13.  
  14. # scipy
  15.  
  16. import numpy as np
  17. import scipy as sp
  18. from scipy.optimize import linprog
  19. a=np.array([[2,1],[-4,5],[-1,2]])
  20. b=np.array([20,10,-2])
  21. c=np.array([1,2])
  22. LB=np.array([0,0])
  23. res=linprog(-c,A_ub=a,b_ub=b)
  24. print(res)
  25.  
  26.  
  27. # Classes
  28.  
  29. import numpy as np
  30. class Point:
  31.     def __init__(self,id,x=0,y=0):
  32.         self.id = id
  33.         self.x = x
  34.         self.y = y
  35.  
  36.     def setId(self,id):
  37.         self.id = id
  38.  
  39.     def setX(self,x):
  40.         self.x = x    
  41.  
  42.     def setY(self,y):
  43.         self.y = y
  44.  
  45.     def getId(self):
  46.         print(f'the id is {self.id}')
  47.  
  48.     def getX(self):
  49.         print(f'the x of this point is {self.x}.')
  50.  
  51.     def getY(self):
  52.         print(f'the y of this point is {self.y}.')
  53.  
  54.     def deplacer (self,dx,dy):
  55.         self.x = self.x + dx
  56.         self.y = self.y + dy
  57.  
  58.     def dist (self,point):
  59.         d=np.sqrt((self.x-point.x)**2+(self.y-point.y)**2)
  60.         print(f'the distance between 2 points is {d}.')
  61.        
  62.        
  63. # tower of hanoi
  64. def hanoi(n, source, helper, target):
  65.     if n > 0:
  66.         # move tower of size n - 1 to helper:
  67.         hanoi(n - 1, source, target, helper)
  68.         # move disk from source peg to target peg
  69.         if source:
  70.             target.append(source.pop())
  71.         # move tower of size n-1 from helper to target
  72.         hanoi(n - 1, helper, source, target)
  73.        
  74. source = [3,2,1]
  75. target = []
  76. helper = []
  77. hanoi(len(source),source,helper,target)
  78.  
  79. print(source, helper, target)
  80.  
  81.  
  82.  
  83.  
  84.  
  85. # PGCD
  86.  
  87. def pgcd(a, b):
  88.     a, b = b % a, a
  89.     if a == 0:
  90.         # BASE CASE
  91.         return b
  92.     else:
  93.         # RECURSIVE CASE
  94.         return pgcd(a, b)
  95.  
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement