-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
154 lines (119 loc) · 4.2 KB
/
app.py
File metadata and controls
154 lines (119 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
# 🟢 GET endpoint — Hello World
@app.route("/", methods=["GET"])
def hello_world():
return "Hello World!"
# 🔵 POST endpoint — Echo
@app.route("/echo", methods=["POST"])
def echo():
data = request.get_json()
return jsonify({"you_sent": data})
# 🟡 Proxy endpoint — PokeAPI proxy for any Pokémon
@app.route("/pokemon/<name>", methods=["GET"])
def get_pokemon(name):
url = f"https://pokeapi.co/api/v2/pokemon/{name}"
response = requests.get(url)
if response.status_code == 200:
return jsonify(response.json()), 200
else:
return jsonify({"error": "Pokémon not found"}), response.status_code
@app.route("/multiply", methods=["POST"])
def multiply_matrices():
data = request.get_json()
try:
A = data["A"]
B = data["B"]
C = data.get("C") # optional third matrix
import numpy as np
result = np.matmul(A, B)
if C:
result = np.matmul(result, C)
return jsonify({"result": result.tolist()}), 200 # ✅ convert ndarray to list
except Exception as e:
return jsonify({"error": str(e)}), 400
@app.route("/power", methods=["POST"])
def power():
data = request.get_json()
base = data.get("base")
exponent = data.get("exponent")
if not isinstance(base, (int, float)) or not isinstance(exponent, int):
return jsonify({"error": "Base must be a number, exponent must be an integer."}), 400
result = 1
b = base
e = exponent
if e < 0:
b = 1 / b
e = -e
while e > 0:
if e % 2 == 1:
result *= b
b *= b
e //= 2
return jsonify({"result": result}), 200
@app.route("/matrix_power", methods=["POST"])
def matrix_power():
data = request.get_json()
matrix = data.get("matrix")
exponent = data.get("exponent")
if not isinstance(matrix, list) or not isinstance(exponent, int):
return jsonify({"error": "Matrix must be a list of lists and exponent must be an integer."}), 400
try:
A = np.array(matrix)
if A.shape[0] != A.shape[1]:
return jsonify({"error": "Matrix must be square (same number of rows and columns)."}), 400
def matrix_exp(A, exp):
result = np.identity(A.shape[0])
if exp < 0:
A = np.linalg.inv(A)
exp = -exp
while exp > 0:
if exp % 2 == 1:
result = np.matmul(result, A)
A = np.matmul(A, A)
exp //= 2
return result
powered = matrix_exp(A, exponent)
return jsonify({"result": powered.tolist()}), 200
except Exception as e:
return jsonify({"error": str(e)}), 400
# Plain Python matrix multiplication
def matrix_multiply(A, B):
rows_A, cols_A = len(A), len(A[0])
rows_B, cols_B = len(B), len(B[0])
if cols_A != rows_B:
raise ValueError("Matrix dimensions do not allow multiplication")
result = [[0 for _ in range(cols_B)] for _ in range(rows_A)]
for i in range(rows_A):
for j in range(cols_B):
for k in range(cols_A):
result[i][j] += A[i][k] * B[k][j]
return result
# Plain Python matrix exponentiation
def matrix_power_plain_impl(matrix, exponent):
if len(matrix) != len(matrix[0]):
raise ValueError("Matrix must be square")
size = len(matrix)
result = [[1 if i == j else 0 for j in range(size)] for i in range(size)]
while exponent > 0:
if exponent % 2 == 1:
result = matrix_multiply(result, matrix)
matrix = matrix_multiply(matrix, matrix)
exponent //= 2
return result
# Route using plain Python implementation
@app.route("/matrix_power_plain", methods=["POST"])
def matrix_power_plain():
data = request.get_json()
try:
matrix = data["matrix"]
exponent = int(data["exponent"])
result = matrix_power_plain_impl(matrix, exponent)
return jsonify({"result": result}), 200
except Exception as e:
return jsonify({"error": str(e)}), 400
def power_of_two(exponent):
return 2 ** exponent
if __name__ == "__main__":
app.run(debug=True)