I'm trying to get sympy to pprint matrices with trig functions with the first letter instead of the shortened term in order to save space. So sin(a_1) will look like sa_1. Right now I'm printing to text then running a find and replace. (I'm new to programming.) So far, this is what does not work:
from sympy import sin as s
from sympy import cos as c
# declaring symbolic variables:
sin, cos, = sym.symbols('s, c')
#An example Matrix
T = Matrix([[c(theta), -s(theta), 0, a],
[s(theta) * c(alpha), c(theta) * c(alpha), -s(alpha), -s(alpha) * d],
[s(theta) * s(alpha), c(theta) * s(alpha), c(alpha), c(alpha) * d],
[0, 0, 0, 1]])
#T04 was a sympy symbol matrix solution
T_000 = str(print_latex(T04))
T_000 = str(T04)
T_000 = T_000.replace('sin', 's')
T_000 = T_000.replace('cos', 'c')
print('T000\n')
pprint(T_000)
T_001 = Matrix([[(-s(theta_1) * s(theta_2) + c(theta_1) * c(theta_2)) * c(theta_3) + (
-s(theta_1) * c(theta_2) - s(theta_2) * c(theta_1)) * s(theta_3),
-(-s(theta_1) * s(theta_2) + c(theta_1) * c(theta_2)) * s(theta_3) + (
-s(theta_1) * c(theta_2) - s(theta_2) * c(theta_1)) * c(theta_3), 0,
L_1 * c(theta_1) + L_2 * (-s(theta_1) * s(theta_2) + c(theta_1) * c(theta_2))], [
(-s(theta_1) * s(theta_2) + c(theta_1) * c(theta_2)) * s(theta_3) + (
s(theta_1) * c(theta_2) + s(theta_2) * c(theta_1)) * c(theta_3),
(-s(theta_1) * s(theta_2) + c(theta_1) * c(theta_2)) * c(theta_3) - (
s(theta_1) * c(theta_2) + s(theta_2) * c(theta_1)) * s(theta_3), 0,
L_1 * s(theta_1) + L_2 * (s(theta_1) * c(theta_2)
+ s(theta_2) * c(theta_1))], [0, 0, 1, d_4], [0, 0, 0, 1]])
print('\nT001\n')
pprint(T_001)
T_000.replace(sin, s)
print('T000\n', T_000)
It's always printing with the full "sin" and "cos" names.
There are two common ways to replace functions in an expression(in this case the expression is the matrix T). One is to use the replace function, the other is to use the subs function. Any of the two can be used to achieve the same goal of replacing sin/cos with other functions.
In the code below we're replacing the function s (which is an alias to sin) with the undefined function s1 (whose symbolic name is s).
The same happens with c and c1.
from sympy import *
# s,c act as aliases to sin,cos
s = sin
c = cos
# the intended short-hand functions used to replace sin/cos later on
s1 = Function('s')
c1 = Function('c')
a,d,theta,alpha = symbols('a d \\theta \\alpha')
T = Matrix([
[c(theta), -s(theta), 0, a],
[s(theta) * c(alpha), c(theta) * c(alpha), -s(alpha), -s(alpha) * d],
[s(theta) * s(alpha), c(theta) * s(alpha), c(alpha), c(alpha) * d],
[0, 0, 0, 1]
])
T_1 = T.replace(s,s1).replace(c,c1)
T_2 = T.subs({s:s1,c:c1})
display(T_1)
display(T_2)
OUTPUT:
The code used in this post is also available here.
Related
Here is my attempt to solve a system of ordinary differential equations given some initial conditions and constraints. While I have figured out how to solve the ODE using odeint from Scipy package without the constraints, I do not know how to incorporate the existence of two constraints on the parameters of the function (which are commented out in the code).
I would truly appreciate your help.
Note: any undefined variable in the code is actually some fixed scalar.
import numpy as np
from scipy.integrate import odeint
#initial conditions
y0, u0 = ([2.1E19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2.1E19, 0, 0, 0])
def lhs(y):
"""This function extracts all 13 components of the solution y"""
return y[0], y[1], y[2], y[3], y[4], y[5], y[6], y[7], y[8], y[9], y[10], y[11], y[12]
def eqns(time_val, y, f_func, g_func, h_func, p_func, diff_eq_solutions_num):
"This is the system of equations that needs to be solved numerically"
var1, var2, var3, var4, var5, var6, var7, var8, var9, var10, var11, var12, var13 = lhs(y)
#var1
r0 = -(K2 * var11 * var1) - ((K4+K5) * var11 * var1) + (0.5 * (K6+K7) * var2**2) + (
2*K12*var11*var8) - (K11 * var1 * (var5+var6+var7)) + (Qa*var2*var9) + (Qb*var3*var1) + (
Qc*var4*var1) + (A4*var7) + (A5*var6) - (
(K14+K15) * var11 * var1) - (f_func(time_val) * var1) - (
g_func(time_val) * var1) - (G1+G2+G3+G4) * h_func(time_val)
#var2
r1 = (A2*var3) - ((K6+K7) * var2**2) - (K11 * var2 * (var5+var6+var7)) + (
K3 * f1 * var11 *
(var5+var6+var7)) + (K14*var11*var1) - (Qa*var9*var2) + G2 * f1 * h_func(time_val)
#var3
r2 = (A1*var4) - (A2*var3) + (0.5 * K6 * var2**2) - (K11 * var3 * (var5+var6+var7)) - (
Qb*var3*var1) + (K10 * var1 * var9**2) + (K15*var11*var1) + (
K3 * f2 * var11 * (var5+var6+var7)) + G2 * f2 * h_func(time_val)
#var4
r3 = (K3 * f3 * var11 * (var5+var6+var7)) - (A1*var4) - (Qc*var4*var1) + (K7*var2*var2) - (
K11 * var4 * (var5+var6+var7)) + (f_func(time_val) * var1) + G2 * f3 * h_func(time_val)
#var5
r4 = (K2*fi1*var11*var1) - (K3*var11*var5) - (K8*var11*var5) - (K11*var1*var5) + (A5*var6) + (
A4*var7) + (Qi1*var7*var1) + (Qi2*var7*NO2) + (fi1 * g_func(time_val) *
var1) + G1 * fi1 * h_func(time_val)
#var6
r5 = (K2*fi2*var11*var1) - (K3*var11*var6) - (A5*var6) - (K8*var11*var6) - (K11*var1*var5) + (
fi2 * g_func(time_val) * var1) + G1 * fi3 * h_func(time_val)
#var7
r6 = (K2*fi3*var11*var1) - (K3*var11*var7) - (A4*var7) - (Qi1*var7*var1) - (K8*var11*var7) - (
Qi2*var7*NO2) + (fi3 * g_func(time_val) * var1) + G1 * fi2 * h_func(time_val)
#var8
r7 = (K11 * (var5+var6+var7) * (var1+var2+var3+var4)) - (K12*var11*var8)
#var9
r8 = (K4*var11*var1) + (2*K5*var11*var1) + (2 * K8 * var11 * (var5+var6+var7)) - (
K9*var11*var9) - (K10 * var1 * var9**2) + (K13*var11*var10) + G4 * h_func(time_val)
#var10
r9 = (K4*var11*var1) + (K9*var11*var9) - (K13*var11*var10) + G3 * h_func(time_val)
r10 = -(K8 * var11 * (var5+var6+var7)) + (K9*var11*var9) - (K13*var11*var10) - (
K12*var11*var8) - (K3 * var11 *
(var5+var6+var7)) + p_func(time_val) + G5 * h_func(time_val) + (
K4*var11*var1) + (K2*var11*var1)
r11 = (A1*var4) - (4 * np.pi * var12 / 3E-11)
r12 = (A4*var7) - (4 * np.pi * var13 / 3E-11)
#These are the constraints I would like to incorporate into system of differential equations
# 2.1E19 = var1 + var2 + var3 + var4 + var5 + var6 + var7 + var8 + var9 + var10
# 0 = var5 + var6 + var7 + var8 + var10 - var11
return [r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12]
def integrator(soln, f_func, g_func, p_func, h_func, i, j, diff_eq_solutions_num):
args = (f_func[i][j], g_func[i][j], p_func[i][j], h_func[i][j], diff_eq_solutions_num)
t = np.linspace(0, 2E-6, 100000)
soln[i][j] = odeint(eqns, y0, t, args=args, tfirst=True, printmessg=True, ixpr=True)
I want to convert from sRGB D65 to CIELab D50. I'm aware of the Bruce Lindbloom functions and calculator but I just want to be sure if I am doing the calculations right.
Starting from a values of sR/100, sG/80, sB/20 and D65, would the following workflow be correct?
sRGB D65 -> XYZ -> Bradford Chromatic adaptation to D50 -> CIE Lab D50 = 34.99, 0.51, 31.35.
There is something not working in the chromatic adaption part of your implementation.
Using colour and a manual conversion:
>>> import colour
>>> import numpy as np
>>> RGB = np.array([100, 80, 20]) / 255
>>> D50 = colour.CCS_ILLUMINANTS['cie_2_1931']['D50']
>>> XYZ = colour.sRGB_to_XYZ(RGB, illuminant=D50)
>>> print(colour.XYZ_to_Lab(XYZ, illuminant=D50))
[ 35.31471609 3.63177851 37.28158403]
And with the Automatic Colour Conversion graph:
>>> colour.convert(RGB, 'sRGB', 'CIE Lab', illuminant=D50) * 100
array([ 35.31471609, 3.63177851, 37.28158403]
And an alternative path that does not use the colour.sRGB_to_XYZ definition and seem to match yours:
>>> colour.convert(RGB, 'Output-Referred RGB', 'CIE Lab', illuminant=D50, verbose={'mode': 'short'}) * 100
===============================================================================
* *
* [ Conversion Path ] *
* *
* "cctf_decoding" --> "RGB_to_XYZ" --> "XYZ_to_Lab" *
* *
===============================================================================
array([ 34.99753019, 0.50577795, 31.35732344])
What is happening though here is that the conversion from RGB to CIE XYZ tristimulus values does not perform any chromatic adaptation between D65 and D50 because the illuminant argument is not matched by the colour.RGB_to_XYZ definition. The proper way to do it would be to specify illuminant_RGB for the RGB side although it defaults to D65 and illuminant_XYZ for the CIE XYZ side:
>>> colour.convert(RGB, 'Output-Referred RGB', 'CIE Lab', illuminant_XYZ=D50, illuminant=D50, verbose={'mode': 'short'}) * 100
===============================================================================
* *
* [ Conversion Path ] *
* *
* "cctf_decoding" --> "RGB_to_XYZ" --> "XYZ_to_Lab" *
* *
===============================================================================
array([ 35.31471609, 3.63177851, 37.28158403])
Now we match the expected conversion result. Here is the verbose conversion so that you can check the intermediate values:
>>> colour.convert(RGB, 'Output-Referred RGB', 'CIE Lab', RGB_to_XYZ={'illuminant_XYZ': D50}, XYZ_to_Lab={'illuminant': D50}, verbose={'mode': 'Extended'}) * 100
===================================================================================
* *
* [ Conversion Path ] *
* *
* "cctf_decoding" --> "RGB_to_XYZ" --> "XYZ_to_Lab" *
* *
===================================================================================
===================================================================================
* *
* [ "cctf_decoding" ] *
* *
* [ Signature ] *
* *
* <Signature (value, function='sRGB', **kwargs)> *
* *
* [ Documentation ] *
* *
* Decodes non-linear :math:`R'G'B'` values to linear :math:`RGB` values using *
* given decoding colour component transfer function (Decoding CCTF). *
* *
* Parameters *
* ---------- *
* value : numeric or array_like *
* Non-linear :math:`R'G'B'` values. *
* function : unicode, optional *
* {:attr:`colour.CCTF_DECODINGS`}, *
* Computation function. *
* *
* Other Parameters *
* ---------------- *
* \**kwargs : dict, optional *
* Keywords arguments for the relevant decoding CCTF of the *
* :attr:`colour.CCTF_DECODINGS` attribute collection. *
* *
* Warnings *
* -------- *
* For *ITU-R BT.2100*, only the electro-optical transfer functions *
* (EOTFs / EOCFs) are exposed by this definition, please refer to the *
* :func:`colour.oetf_inverse` definition for the inverse opto-electronic *
* transfer functions (OETF / OECF). *
* *
* Returns *
* ------- *
* numeric or ndarray *
* Linear :math:`RGB` values. *
* *
* Examples *
* -------- *
* >>> cctf_decoding(0.391006842619746, function='PLog', log_reference=400) *
* ... # doctest: +ELLIPSIS *
* 0.1... *
* >>> cctf_decoding(0.182011532850008, function='ST 2084', L_p=1000) *
* ... # doctest: +ELLIPSIS *
* 0.1... *
* >>> cctf_decoding( # doctest: +ELLIPSIS *
* ... 0.461356129500442, function='ITU-R BT.1886') *
* 0.1... *
* *
* [ Conversion Output ] *
* *
* [ 0.12743768 0.08021982 0.00699541] *
* *
===================================================================================
===================================================================================
* *
* [ "RGB_to_XYZ" ] *
* *
* [ Signature ] *
* *
* <Signature (RGB, illuminant_RGB, illuminant_XYZ, matrix_RGB_to_XYZ, *
* chromatic_adaptation_transform='CAT02', cctf_decoding=None)> *
* *
* [ Filtered Arguments ] *
* *
* {'cctf_decoding': {'return': array([ 0.12743768, 0.08021982, *
* 0.00699541])}, *
* 'illuminant_XYZ': array([ 0.3457, 0.3585])} *
* *
* [ Documentation ] *
* *
* Converts given *RGB* colourspace array to *CIE XYZ* tristimulus values. *
* *
* Parameters *
* ---------- *
* RGB : array_like *
* *RGB* colourspace array. *
* illuminant_RGB : array_like *
* *CIE xy* chromaticity coordinates or *CIE xyY* colourspace array of the *
* *illuminant* for the input *RGB* colourspace array. *
* illuminant_XYZ : array_like *
* *CIE xy* chromaticity coordinates or *CIE xyY* colourspace array of the *
* *illuminant* for the output *CIE XYZ* tristimulus values. *
* matrix_RGB_to_XYZ : array_like *
* Matrix converting the *RGB* colourspace array to *CIE XYZ* tristimulus *
* values, i.e. the *Normalised Primary Matrix* (NPM). *
* chromatic_adaptation_transform : unicode, optional *
* **{'CAT02', 'XYZ Scaling', 'Von Kries', 'Bradford', 'Sharp', *
* 'Fairchild', 'CMCCAT97', 'CMCCAT2000', 'CAT02 Brill 2008', *
* 'Bianco 2010', 'Bianco PC 2010', None}**, *
* *Chromatic adaptation* transform, if *None* no chromatic adaptation is *
* performed. *
* cctf_decoding : object, optional *
* Decoding colour component transfer function (Decoding CCTF) or *
* electro-optical transfer function (EOTF / EOCF). *
* *
* Returns *
* ------- *
* ndarray *
* *CIE XYZ* tristimulus values. *
* *
* Notes *
* ----- *
* *
* +--------------------+-----------------------+---------------+ *
* | **Domain** | **Scale - Reference** | **Scale - 1** | *
* +====================+=======================+===============+ *
* | ``RGB`` | [0, 1] | [0, 1] | *
* +--------------------+-----------------------+---------------+ *
* | ``illuminant_XYZ`` | [0, 1] | [0, 1] | *
* +--------------------+-----------------------+---------------+ *
* | ``illuminant_RGB`` | [0, 1] | [0, 1] | *
* +--------------------+-----------------------+---------------+ *
* *
* +--------------------+-----------------------+---------------+ *
* | **Range** | **Scale - Reference** | **Scale - 1** | *
* +====================+=======================+===============+ *
* | ``XYZ`` | [0, 1] | [0, 1] | *
* +--------------------+-----------------------+---------------+ *
* *
* Examples *
* -------- *
* >>> RGB = np.array([0.45595571, 0.03039702, 0.04087245]) *
* >>> illuminant_RGB = np.array([0.31270, 0.32900]) *
* >>> illuminant_XYZ = np.array([0.34570, 0.35850]) *
* >>> chromatic_adaptation_transform = 'Bradford' *
* >>> matrix_RGB_to_XYZ = np.array( *
* ... [[0.41240000, 0.35760000, 0.18050000], *
* ... [0.21260000, 0.71520000, 0.07220000], *
* ... [0.01930000, 0.11920000, 0.95050000]] *
* ... ) *
* >>> RGB_to_XYZ(RGB, illuminant_RGB, illuminant_XYZ, matrix_RGB_to_XYZ, *
* ... chromatic_adaptation_transform) # doctest: +ELLIPSIS *
* array([ 0.2163881..., 0.1257 , 0.0384749...]) *
* *
* [ Conversion Output ] *
* *
* [ 0.08765592 0.08656689 0.01383652] *
* *
===================================================================================
===================================================================================
* *
* [ "XYZ_to_Lab" ] *
* *
* [ Signature ] *
* *
* <Signature (XYZ, illuminant=array([ 0.3127, 0.329 ]))> *
* *
* [ Filtered Arguments ] *
* *
* {'illuminant': array([ 0.3457, 0.3585])} *
* *
* [ Documentation ] *
* *
* Converts from *CIE XYZ* tristimulus values to *CIE L\*a\*b\** *
* colourspace. *
* *
* Parameters *
* ---------- *
* XYZ : array_like *
* *CIE XYZ* tristimulus values. *
* illuminant : array_like, optional *
* Reference *illuminant* *CIE xy* chromaticity coordinates or *CIE xyY* *
* colourspace array. *
* *
* Returns *
* ------- *
* ndarray *
* *CIE L\*a\*b\** colourspace array. *
* *
* Notes *
* ----- *
* *
* +----------------+-----------------------+-----------------+ *
* | **Domain** | **Scale - Reference** | **Scale - 1** | *
* +================+=======================+=================+ *
* | ``XYZ`` | [0, 1] | [0, 1] | *
* +----------------+-----------------------+-----------------+ *
* | ``illuminant`` | [0, 1] | [0, 1] | *
* +----------------+-----------------------+-----------------+ *
* *
* +----------------+-----------------------+-----------------+ *
* | **Range** | **Scale - Reference** | **Scale - 1** | *
* +================+=======================+=================+ *
* | ``Lab`` | ``L`` : [0, 100] | ``L`` : [0, 1] | *
* | | | | *
* | | ``a`` : [-100, 100] | ``a`` : [-1, 1] | *
* | | | | *
* | | ``b`` : [-100, 100] | ``b`` : [-1, 1] | *
* +----------------+-----------------------+-----------------+ *
* *
* References *
* ---------- *
* :cite:`CIETC1-482004m` *
* *
* Examples *
* -------- *
* >>> import numpy as np *
* >>> XYZ = np.array([0.20654008, 0.12197225, 0.05136952]) *
* >>> XYZ_to_Lab(XYZ) # doctest: +ELLIPSIS *
* array([ 41.5278752..., 52.6385830..., 26.9231792...]) *
* *
* [ Conversion Output ] *
* *
* [ 0.35314716 0.03631779 0.37281584] *
* *
===================================================================================
array([ 35.31471609, 3.63177851, 37.28158403])
check :: String -> Int -> Int -> IO()
check map len n = do
let current = searchMap map '#'
if (flood map current Up len n)
then printf "\nThis map is solvable. \n"
else printf "\nThis map is unsolvable. \n"
flood :: String -> Int -> Direction -> Int -> Int -> Bool
flood map current direct len n = do
let upward = current - n
let downward = current + n
let leftward = current - 2
let rightward = current + 2
if (current < 0 || current > len || (map !! current) == '*' || (map !! current) == 'x')
then False
else if (map !! current) == 't'
then True
else do
case direct of
Up -> do
let newMap = replaceSign map current "x"
(flood map upward Up len n) || (flood map leftward Leftx len n) || (flood map rightward Rightx len n)
Down -> do
let newMap = replaceSign map current "x"
(flood map downward Down len n) || (flood map leftward Leftx len n) || (flood map rightward Rightx len n)
Leftx -> do
let row = findRow current n 0
if current < (row * n)
then False
else do
let newMap = replaceSign map current "x"
(flood map upward Up len n) || (flood map downward Down len n) || (flood map leftward Leftx len n)
Rightx -> do
let row = findRow current n 0
if current >= ((row + 1) * n)
then False
else do
let newMap = replaceSign map current "x"
(flood map upward Up len n) || (flood map downward Down len n) || (flood map rightward Rightx len n)
I am working on a project which needs to make a minigame. The player will need to input a map.
Something like this:
* * * * * - - - - - - - - - - - - - - - - - - - * * * * *
* * * * * b - - - - - - - - - - - - - - - - - b * * * * *
* * * * * - * * * * * * * * * * * * * * * * * - * * * * *
* * * * * - * * - - - - * * * * * - - - - * * - * * * * *
* * * * * - * * - y y - * * * * * - y y - * * - * * * * *
* * * * * - * * - - - - * * * * * - - - - * * - * * * * *
* * * * * - * * * * * * - - b - - * * * * * * - * * * * *
* * * * * - * * * * * * - * * * - * * * * * * - * * * * *
# - - - - - * * * * * * - * * * - * * * * * * p - - - - t
* * * * * - * * * * * * - * * * - * * * * * * - * * * * *
* * * * * - - - - - - - - * * * - - - - - - - - * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
I struggling in a function to determine whether the map is solvable or not.
In the code, current means the current location the program is trying. len is the length of the String holding the map. n is the length of one line of the map. In the map, # means the ball. - means roads that the ball can move on. b means bonus block (not related to this problem). * means obstacle. t means target p/o/y means blocks with special use (ball can stop there). My current approach is to use a function try to try recursively. The function will turn roads that have already tried into x and the function will return False if it hit x. When function hits obstacles (*) or boundaries of map it will try another two directions. Eg. if you are currently moving Up. Then it will try Leftx and Rightx. If it hits special blocks (p/o/y), it will try the other three directions. Eg. if you are currently moving Rightx, then it will try Up, Down, and Rightx. However, the program returns "Exception: stack overflow". Is it something wrong with my algorithm? How can I fix this?
The problem is that once your search has proceeded all the way right and up, so the map looks like this, with the current position the leftmost - in the first row:
* * * * * - - - - - - - - - - - - - - - - - - - * * * * *
* * * * * x - - - - - - - - - - - - - - - - - b * * * * *
* * * * * x * * * * * * * * * * * * * * * * * - * * * * *
* * * * * x * * - - - - * * * * * - - - - * * - * * * * *
* * * * * x * * - y y - * * * * * - y y - * * - * * * * *
* * * * * x * * - - - - * * * * * - - - - * * - * * * * *
* * * * * x * * * * * * - - b - - * * * * * * - * * * * *
* * * * * x * * * * * * - * * * - * * * * * * - * * * * *
x x x x x x * * * * * * - * * * - * * * * * * p - - - - t
* * * * * - * * * * * * - * * * - * * * * * * - * * * * *
* * * * * - - - - - - - - * * * - - - - - - - - * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
you enter the following loop with no further x marks made to the map:
try map 10 Up ... =
-- at top row, so we evaluate:
(try map current Leftx len n) || ...
try map 10 Leftx ... =
-- star to the left, so we evaluate:
(try map current Up len n) || ...
try map 10 Up ... =
-- at top row, so we evaluate:
(try map current Leftx len n) || ...
-- and we're stuck in an infinite loop
The stack eventually overflows because Haskell is keeping track of all those right-hand sides of the || operator, though it never gets a chance to try any of them.
I think you'll need to either modify your algorithm to mark walls you've already tried, or maybe switch from working with a single path to flood filling all the non-stars -- if you hit the target while flood fillling, the maze is solvable.
What I basically want, is comparing a timevalue (t1 and tuit)(in hours) to determine which method to use to calculate 'S' and 'k' in a function called 'stijghoogteverlaging'. Then a fitted curve can be made with those values.
I tried multiple things, like putting 'return s' underneath both s-methods.
if t1[i] < tuit:
s = Q / (4 * np.pi * k * D) * exp1(S * r**2 / (4 * k * D * t))
return s
else:
s = Q / (4 * np.pi * k * D) * ((exp1(S * r**2 / (4 * k * D * t))) - (exp1(S * r**2 / (4 * k * D * (t - tuit)))))
return s
But then I got a wrong fitted curve as can be seen in the image below.
Now I tried putting only one 'return s', but then it takes forever to calculate and I have to interrupt the kernel.
data = read_csv("pompproef_data.csv", sep = ';')
pb1 = data.iloc[1:,1].values-1.87
pb2 = data.iloc[1:,2].values-1.86
t1 = data.iloc[1:,0].values / (60*24)
volume = 10/1000 #m3
duur = [128,136, 150, 137, 143, 141] #seconden
totaal = np.sum(duur)
debiet = (((len(duur) * volume)/totaal)) * (60*60*24) #m3/d
print(debiet)
print(t1)
print(pb1)
tuit = 15/(24*60)
D = 2.0
Q = debiet
def stijghoogteverlaging(t, k, S):
for i in range(len(t1)):
if t1[i] < tuit:
s = Q / (4 * np.pi * k * D) * exp1(S * r**2 / (4 * k * D * t))
else:
s = Q / (4 * np.pi * k * D) * ((exp1(S * r**2 / (4 * k * D * t))) - (exp1(S * r**2 / (4 * k * D * (t - tuit)))))
return s
r = 4.0 #afstand peilbuis1 tot put
poptpb1, pcovpb1 = curve_fit(stijghoogteverlaging, t1, pb1, p0=[100, 1e-25], maxfev = 10000000)
print('optimale waarde van k voor peilbuis1:', poptpb1[0])
print('optimale waarde van S voor peilbuis1:', poptpb1[1])
tijd = data.iloc[1:,0].values
t = np.linspace(0.00069*(24*60), 0.021*(24*60), 1000)
s1 = stijghoogteverlaging(t, poptpb1[0], poptpb1[1])
plt.plot(tijd, pb1, 'r.', label = 'Gemeten bij 4 meter')
plt.plot(t, s1, 'b', label = 'fitted bij 4 m')
Does anyone have a solution?
Used values for t1 and pb1:
Plot with a wrong fitted curve(time in minutes).
The function stijghoogteverlaging is performing a nonsense operation over and over:
def stijghoogteverlaging(t, k, S):
for i in range(len(t1)):
if t1[i] < tuit:
s = Q / (4 * np.pi * k * D) * exp1(S * r**2 / (4 * k * D * t))
else:
s = Q / (4 * np.pi * k * D) * ((exp1(S * r**2 / (4 * k * D * t))) - (exp1(S * r**2 / (4 * k * D * (t - tuit)))))
return s
You are iterating len(t1) times, and at each iteration, you are computing the full vectorized value of s each and every time. That means that you are computing len(t)**2 values per call, and using a Python for loop as your outer loop to do it. As a minor point, you are accessing the x-data as the global variable t1 instead of the local value t, which gets passed in.
Your function should probably look more like this:
def stijghoogteverlaging(t, k, S):
return np.where(t < tuit,
Q / (4 * np.pi * k * D) * exp1(S * r**2 / (4 * k * D * t)),
Q / (4 * np.pi * k * D) * ((exp1(S * r**2 / (4 * k * D * t))) - (exp1(S * r**2 / (4 * k * D * (t - tuit)))))
)
This computes len(t) * 2 values per call, not len(t)**2, and selects a value from the appropriate result for each value of t.
Here is my code but it is not working as expected
def printFlippedTriangle(width):
for i in range(0, width):
for J in range(0, width-i):
print(" ", end=" ") # single line
for j in range(0,i):
print(" "+"* ", end=" ") # single line
j=j-1
print("*")
Am getting this:
*
* * * * *
* * * * * * *
* * * * * * *
* * * * *
am suppose to get:
*
* *
* * *
* * * *
* * * * *
Any idea and or suggestion will be appreciated
This will get the job done, and in a single loop too!
def triangle(w):
for i in range(0, w):
print(' ' * ((w - i - 1) * 2), end='') # spaces for each row
print('* ' * (i + 1), end='') # * for each row
print() # new line
>>> triangle(5)
*
* *
* * *
* * * *
* * * * *
Each row needs width - rowNumber - 1 spaces and rowNumber + 1 asterisks when starting from 0