How to fix a subtraction operation in Python - python-3.x

I am trying a simple subtraction operation: water_to_add should be dna_to_add minus final_volume which is 60.0. However the results for dna_to_add and water_to_add have been the same. Why my "minus" operation for water_to_add is not working?
BTW, dna_to_add is being calculated correctly.
concentration_ng_ul = input("Enter Concentration (ng/ul): ")
size = input("Enter Size (bp): ")
c= float(concentration_ng_ul)
s = float(size)
c_ug_ul = c/1000.0
pmol_DNA_ul = c_ug_ul*1515.15*1/s
fmol_DNA_ul = pmol_DNA_ul/1000.0
fmol_DNA_goal = 20.0
final_volume = 60.0
dna_to_add = (fmol_DNA_goal)*(final_volume/fmol_DNA_ul)
print('DNA_ul',dna_to_add)
water_to_add = (dna_to_add)-(final_volume)
print('Water_ul',water_to_add)

Related

Plot output differences between python and julia

I am trying to use julai as main language for my work. But I find that this plot is different than python (Which outputs the right plot)
Here is the python code and output
import numpy as np
import math
import matplotlib.pyplot as plt
u = 9.27*10**(-21)
k = 1.38*10**(-16)
j2 = 7/2
nrr = 780
h = 1000
na = 6*10**(23)
rho = 7.842
mgd = 157.25
a = mgd
d = na*rho*u/a
m_f = []
igd = 7.0
for t in range(1,401):
while True:
h1 = h+d*nrr*igd
x2 = (7*u*h1)/(k*t)
x4 = 2*j2
q2 = (x4+1)/x4
m = abs(7*(q2*math.tanh(q2*x2)**-1 - (1/x4)*math.tanh(x2/x4)**-1))
if abs(m - igd) < 10**(-12):
break
else:
igd = m
m_f.append(abs(m))
plt.plot(range(1,401), m_f)
plt.savefig("Py_plot.pdf")
and it gives the following right plot
The right plot as expected
But when I do the same calculations in julia it gives different output than python, here is my julia code
using Plots
u = 9.27*10^(-21)
k = 1.38*10^(-16)
j2 = 7/2
nrr = 780
h = 1000
na = 6*10^(23)
rho = 7.842
mgd = 157.25
a = mgd
d = na*rho*u/a
igd = 7.0
m = 0.0
m_f = Float64[]
for t in 1:400
while true
h1 = h+d*nrr*igd
x2 = (7*u*h1)/(k*t)
x4 = 2*j2
q2 = (x4+1)/x4
m = 7*(q2*coth(rad2deg(q2*x2))-(1/x4)*coth(rad2deg(x2/x4)))
if abs(abs(m)-igd) < 10^(-10)
break
else
igd = m
end
end
push!(m_f, abs(m))
end
plot(1:400, m_f)
and this is the unexpected julia output
unexpected wrong output from julia
I wish for help....
Code:
using Plots
const u = 9.27e-21
const k = 1.38e-16
const j2 = 7/2
const nrr = 780
const h = 1000
const na = 6.0e23
const rho = 7.842
const mgd = 157.25
const a = mgd
const d = na*rho*u/a
function plot_graph()
igd = 7.0
m = 0.0
trange = 1:400
m_f = Vector{Float64}(undef, length(trange))
for t in trange
while true
h1 = h+d*nrr*igd
x2 = (7*u*h1)/(k*t)
x4 = 2*j2
q2 = (x4+1)/x4
m = abs(7*(q2*coth(q2*x2)-(1/x4)*coth(x2/x4)))
if isapprox(m, igd, atol = 10^(-10))
break
else
igd = m
end
end
m_f[t] = m
end
plot(trange, m_f)
end
Plot:
Changes for correctness:
Changed na = 6.0*10^(23) to na = 6.0e23.
Since ^ has a higher precedence than *, 10^23 is evaluated first, and since the operands are Int values, the result is also an Int. However, Int (i.e. Int64) can only hold numbers up to approximately 9 * 10^18, so 10^23 overflows and gives a wrong result.
julia> 10^18
1000000000000000000
julia> 10^19 #overflow starts here
-8446744073709551616
julia> 10^23 #and gives a wrong value here too
200376420520689664
6.0e23 avoids this problem by directly using the scientific e-notation to create a literal Float64 value (Float64 can hold this value without overflowing).
Removed the rad2deg calls when calling coth. Julia trigonometric functions by default take radians, so there's no need to make this conversion.
Other changes
Marked all the constants as const, and moved the rest of the code into a function. See Performance tip: Avoid non-constant global variables
Changed the abs(m - igd) < 10^-10 to isapprox(m, igd, atol = 10^-10) which performs basically the same check, but is clearer and more flexible (for eg. if you wanted to change to a relative tolerance rtol later).
Stored the 1:400 as a named variable trange. This is just because it's used multiple times, so it's easier to manage as a variable.
Changed m_f = Float64[] to m_f = Vector{Float64}(undef, length(trange)) (and the push! at the end to an assignment). If the size of the array is known beforehand (as it is in this case), it's better for performance to pre-allocate it with undef values and then assign to it.
Changed u and k also to use the scientific e-notation, for consistency and clarity (thanks to #DNF for suggesting the use of this notation in the comments).

getting marginal effect post-estimation for nested logit using R mlogit package

I have estimated nested logit in R using the mlogit package. However, I encountered some problems when trying to estimate the marginal effect. Below is the code I implemented.
library(mlogit)
# data
data2 = read.csv(file = "neat_num_energy.csv")
new_ener2 <- mlogit.data(
data2,
choice="alter4", shape="long",
alt.var="energy_altern",chid.var="id")
# estimate model
nest2 <- mlogit(
alter4 ~ expendmaint + expendnegy |
educ + sex + ppa_power_sp + hu_price_powersupply +
hu_2price +hu_3price + hu_9price + hu_10price +
hu_11price + hu_12price,
data = data2,
nests = list(
Trad = c('Biomas_Trad', 'Solar_Trad'),
modern = c('Biomas_Modern', 'Solar_Modern')
), unscaled=FALSE)
# create Z variable
z3 <- with(data2, data.frame(
expendnegy = tapply(expendnegy, idx(nest2,2), mean),
expendmaint= tapply(expendmaint, idx(nest2,2), mean),
educ= mean(educ),
sex = mean(sex),
hu_price_powersupply = mean(hu_price_powersupply),
ppa_power_sp = mean(ppa_power_sp),
hu_2price = mean(hu_2price),
hu_3price = mean(hu_3price),
hu_9price = mean(hu_9price),
hu_10price = mean(hu_10price),
hu_11price = mean(hu_11price),
ppa_power_sp = mean(ppa_power_sp),
hu_12price = mean(hu_12price)
))
effects(nest2, covariate = "sex", data = z3, type = "ar")
#> ** Error in Solve.default (H, g[!fixed]): Lapack routine dgesv: #> system is exactly singular:U[6,6] =0.**
My data is in long format with expendmaint and expendnegy being the only alternative specific while every other variable is case specific.
altern4 is a nominal variable representing each alternative

Decrypted image starts good but becomes gray

I am working on a personal project of mine and was wondering how I can fix my issue.
Here is a piece of code I am working on:
f = open('sample.jpeg','rb')
choice = int(input("-> "))
mutableBytes = bytearray(f.read())
f.close()
print(str(mutableBytes) + "SAMPLE")
if choice == 1:
for i in range(len(mutableBytes)):
if mutableBytes[i] < 255:
mutableBytes[i] = mutableBytes[i] + 1
f.close()
print(str(mutableBytes) + "ENCRYPTED")
f = open('samplecopy.jpeg','wb+')
f.write(mutableBytes)
else:
f = open('samplecopy.jpeg','rb')
mutableBytes = bytearray(f.read())
f.close()
for i in range(len(mutableBytes)):
if mutableBytes[i] > 0 and mutableBytes[i]<255:
mutableBytes[i] = mutableBytes[i] - 1
f = open('samplecopy.jpeg','wb+')
f.write(mutableBytes)
print(str(mutableBytes) + "Decrypted")
This should in theory get a picture and encrypt it, after decrypt it. I printed all the bytes and I looked for changes but it looks the same.
Here is the comparison: https://www.diffchecker.com/vTtzGe4O
And here is the image I get vs the original:
(the bottom one is the one I get after decrypting).

Mean of one variable when another variable equals 1 in matlab

I would like to get the mean of my reaction time column when my stimnum column is equal to 1
I am not sure if i can do this with one simple line of code or if i need to do a for loop.
stimnum = randi([1 3], [1 100]);
y = 1 + 1.*randn(1, 100);
rt = (y.^2) +.01;
A = rand(1,100);
correct = A>=0.2;
Data= [stimnum; rt; correct ]';
Data = dataset({ Data, 'Stimnum', 'RT', 'Correct'});
rtmean = mean (Data.RT{Data.Stimnum == 1});

Explanation of algorithm that get the excel column title

According to this topic:
How to convert a column number (e.g. 127) into an excel column (e.g. AA)
I don't understand what is in algorithm:
here
Could someone explain me, what is happening in a while loop?
It is, in effect, "converting" the column number to base 26, where the "digits" are the letters A..Z.
For example, for column 720:
modulo = (720-1)%26 = 17
columnName = 'R'
dividend = (720-17)/26 = 27
modulo = (27-1)%26 = 0
columnName = A+columnName = AR
dividend = (27-0)/26 = 1
modulo = (1-1)%26 = 0
columnName = A + columnName = AAR
dividend = (1-0)/26 = 0
Resulting in AAR.

Resources