Retrieving Values from a Seq based on a condition - arrays

I have the below Seq with me
scala> var al = Seq((1.0, 20.0, 100.0), (2.0, 30.0, 100.0), (1.0, 11.0, 100.0), (1.0, 20.0, 100.0), (1.0, 10.0, 100.0),(2.0,9.0,100.0))
al: Seq[(Double, Double, Double)] = List((1.0,20.0,100.0), (2.0,30.0,100.0), (1.0,11.0,100.0), (1.0,20.0,100.0), (1.0,10.0,100.0), (2.0,9.0,100.0))
How to take from this Seq the first n elements where - sum of the second item is greater than 60 percent of the third item(this will be a constant value)
Expected Output -
scala> Seq((1.0, 20.0, 100.0), (2.0, 30.0, 100.0), (1.0, 11.0, 100.0))
res30: Seq[(Double, Double, Double)] = List((1.0,20.0,100.0), (2.0,30.0,100.0), (1.0,11.0,100.0))
Edit -1
I have done this with some ugly way. But if there is any beautiful functional way of solving this would be great. I would really need to get rid of this counters and is little bit tricky for me.
var counter =0d ; var sum = 0d
var abc :Seq[Double] = for (x <- al) yield {
counter+=1 ;sum = sum + x._2 ;
if (sum > 60) counter else 0
}
println(al.take(abc.filterNot(_==0).min.toInt))

If you're on Scala 2.13.x then you might unfold():
Seq.unfold((al,0.0)){ case (lst,sum) =>
Option.when(lst.nonEmpty && sum < 0.6 * lst.head._3) {
(lst.head, (lst.tail, sum + lst.head._2))
}
}
//res0: Seq[(Double, Double, Double)] =
// List((1.0,20.0,100.0), (2.0,30.0,100.0), (1.0,11.0,100.0))
On earlier Scala versions you could scanLeft() to calculate the sums and use that to count how many elements to take().
val cnt = al.view
.scanLeft(0.0)(_ + _._2)
.takeWhile(_ < 0.6 * al.headOption.getOrElse((0d,0d,0d))._3)
.length
al.take(cnt)

You can use recursive function with accumulator to get needed number of elements:
def getIndex(input: Seq[(Double,Double,Double)], index: Int = 0, sum: Double = 0.0): Int = {
input match {
case Seq() => index
case Seq(head, _#_*) if head._2 + sum > head._3 * 0.6 => index + 1
case Seq(head, tail#_*) => getIndex(tail, index + 1, sum + head._2)
}
}
al.take(getIndex(al))
Or to build a list:
def getList(input: Seq[(Double,Double,Double)], sum: Double = 0.0): List[(Double,Double,Double)] = {
input match {
case Seq() => List.empty
case Seq(head, _#_*) if head._2 + sum > head._3 * 0.6 => head :: List.empty
case Seq(head, tail#_*) => head :: get(tail, sum + head._2)
}
}
getList(al)
As mentioned in comments - restricting al to List can allow using :: which should be more efficient:
def get(input: List[(Double,Double,Double)], sum: Double = 0.0): List[(Double,Double,Double)] = {
input match {
case Seq() => List.empty
case head :: tail if head._2 + sum > head._3 * 0.6 => head :: List.empty
case head :: tail => head :: get(tail, sum + head._2)
}
}

Related

Speed up Value function Iterations in Julia

I am coding a simple value function iteration in Julia. I posted the code below.
# ................................
# Solving the model
function Solver(P,uf,hf)
#unpack r, σrisk, ρ, η, β, θ, nx, m, μ, fhat, ub, lb, tol, maxite, ne = P;
# ..................................................
# Tauchen discretization
ly, Π = mytauch(μ, ρ, η, nx, m); # it is a tauchen discretization
# ..................................................
# states
y = exp.(ly);
b = [LinRange(lb,ub, ne)...];
yb = b .+ y';
p0 = findfirst(x-> x ==0, b);
uhy = uf.(hf(y, fhat));
# ..................................................
# Initial guess for value functions
vr0 = 1 / (1 - β) * uf.((r / (1 + r)) * b .+ y');
vd0 = (1 / (1 - β) * uhy)';
myup(vr,vd) = updateVF(vr, vd, b, yb, uhy, Π, p0, uf, P);
# ..................................................
# Convergence algorithm
dif = 1
ite = 1
vr1, vd1, bopt = (nothing,nothing, nothing);
while dif>tol && ite < maxite
vr1, bopt, vd1 = myup(vr0,vd0)
dif = max(maximum(abs.(vr1-vr0)), maximum(abs.(vd1-vd0)));
ite+=1;
vr0, vd0 = (vr1, vd1);
end
# ..................................................
# Policies functions
vf1, D, δ, q = postVR(vr1,vd1,Π,r)
#bb = repeat(b,1,nx);
vd1 = repeat(vd1,ne,1);
bprime = b[bopt];
PolFun = (vf=vf1 , vr=vr1, vd=vd1, bprime = bprime, q=q,D=D, δ=δ);
out2 = (b= b, y= y,ydef=hf(y, fhat),Π=Π, uhy = uhy, p0 = p0);
return PolFun, out2;
end
function updateVF(vr0, vd0,b,yb,uhy,Π, p0, uf, P)
#unpack r , β, θ, nx,ne = P;
# ..................................................
# Preliminaries
vf0, D0, δ0, q0 = postVR(vr0,vd0,Π,r)
evf0 = vf0*Π';
evd0 = vd0*Π';
qb = q0 .* b;
βevf0 = β*evf0;
# ..................................................
# Value and debt policy function update
# Value of Default
vd1 = uhy' + β*(θ*evf0[p0,:]'+(1-θ)*evd0);
# Value of Repayment
# Method 2: by column
vr1 = Array{Float32,2}(undef, ne, nx);
bopt = Array{CartesianIndex{2},2}(undef, ne, nx);
upvr1!(vr1,bopt,yb,qb,βevf0,nx)
return vr1, map(ii -> ii[1],bopt), vd1
end
function postVR(vr,vd,Π,r)
vf = max.(vr,vd);
D = 1*(vd .> vr);
δ = D*Π';
q = (1/(1+r)) * (1 .- δ);
return vf, D, δ, q;
end
function upvr1!(vr1,bopt,yb,qb,βevf0,nx)
#fastmath #inbounds Threads.#threads for i in 1:nx
vr1[:,i], bopt[:,i] = #views findmax(uf.(max.(0,yb[:,i] .- qb[:,i]')) .+ βevf0[:,i], dims=1);
end
end
The master code is:
P = ( r = 0.017, σrisk = 2.0, ρ = 0.945, η = 0.025, β = 0.953,
θ = 0.282,nx = 21, m = 3, μ = 0.0,fhat = 0.969,
ub = 0, lb = -0.4, tol = 1e-6, maxite = 2000, ne = 251);
uf(x) = -1/x;
hf(y::Array, fhat::Float64) = min.(y, fhat * mean(y));
#time polfun, o2 = Solver(P,uf,hf);
The running tipe after first compilation is ~1 second. How can I speed-up this code? I am asking it because in previous versions of Matlab, in my experience, Julia was 10x faster but with the last version (Matlab 2020, that I tried last week) its performance was close to Julia but as I increase the number of grid-points (ne > 1000) Matlab reported faster. So I suppose that I am not taking advantage of all functionalities of Julia.
Note: The loop for upvr1! overperforms that a broadcasting version
yb = reshape(yb,(ne,nx,1));
βevf01 = PermutedDimsArray(reshape(βevf0,(ne,nx,1)),[3,2,1]);
qb1 = PermutedDimsArray(reshape(qb,(ne,nx,1)),[3,2,1]);
vr1, bopt = #views findmax(uf.(yb .- qb1) .+ βevf01, dims=3)
return vr1[:,:,1], map(ii -> ii[3], bopt)[:,:,1], vd1

Data arrays must have the same length, and match time discretization in dynamic problems error in GEKKO

I want to find the value of the parameter m that minimizes my variable x subject to a system of differential equations. I have the following code
from gekko import GEKKO
def run_model_m(days, population, case, k_val, b_val, u0_val, sigma_val, Kmax0, a_val, c_val):
list_x =[]
list_u =[]
list_Kmax =[]
for i in range(len(days)):
list_xi=[]
list_ui=[]
list_Ki=[]
for j in range(len(days[i])):
#try:
m = GEKKO(remote=False)
#m.time= days[i][j]
eval = np.linspace(days[i][j][0], days[i][j][-1], 100, endpoint=True)
m.time = eval
x_data= population[i][j]
variable= np.linspace(population[i][j][0], population[i][j][-1], 100, endpoint=True)
x = m.Var(value=population[i][j][0], lb=0)
sigma= m.Param(sigma_val)
d = m.Param(c_val)
k = m.Param(k_val)
b = m.Param(b_val)
r = m.Param(a_val)
step = np.ones(len(eval))
step= 0.2*step
step[0]=1
m_param = m.CV(value=1, lb=0, ub=1, integer=True); m_param.STATUS=1
u = m.Var(value=u0_val, lb=0, ub=1)
#m.free(u)
a = m.Param(a_val)
c= m.Param(c_val)
Kmax= m.Param(Kmax0)
if case == 'case0':
m.Equations([x.dt()== x*(r*(1-x/(Kmax))-m_param/(k+b*u)-d), u.dt()== sigma*(m_param*b/((k+b*u)**2))])
elif case == 'case4':
m.Equations([x.dt()== x*(r*(1-u**2)*(1-x/(Kmax))-m_param/(k+b*u)-d), u.dt() == sigma*(-2*u*r*(1-x/(Kmax))+(b*m_param)/(b*u+k)**2)])
p = np.zeros(len(eval))
p[-1] = 1.0
final = m.Param(value=p)
m.Obj(x)
m.options.IMODE = 6
m.options.MAX_ITER=15000
m.options.SOLVER=1
# optimize
m.solve(disp=False, GUI=False)
#m.open_folder(dataset_path+'inf')
list_xi.append(x.value)
list_ui.append(u.value)
list_Ki.append(m_param.value)
list_x.append(list_xi)
list_Kmax.append(list_Ki)
list_u.append(list_ui)
return list_x, list_u, list_Kmax, m.options.OBJFCNVAL
scaled_days[i][j] =[-7.0, 42.0, 83.0, 125.0, 167.0, 217.0, 258.0, 300.0, 342.0]
scaled_pop[i][j] = [0.01762491277346285, 0.020592540360308997, 0.017870838266697213, 0.01690069378982034,0.015512320147187675,0.01506701796298272,0.014096420738841563,0.013991224004743027,0.010543380664478205]
k0,b0,group, case0, u0, sigma0, K0, a0, c0 = (100, 20, 'Size3, Inc', 'case0', 0.1, 0.05, 2, 0, 0.01)
list_x2, list_u2, list_Kmax2,final =run_model_m(days=[[scaled_days[i][j]]], population=
[[scaled_pop[i][j]]],case=case1, k_val=list_b1[i0][0], b_val=b1, u0_val=list_u1[i0][j0],
sigma_val=sigma1, Kmax0=K1, a_val=list_Kmax1[0][0], c_val=c1)
I get the error Data arrays must have the same length, and match time discretization in dynamic problems error but I don't understand why. I have tried making x and m_param arrays, with x=m.Var, m_param =m.MV... But still get the same error, even if they are all arrays of the same length. Is this the right way to find the solution of the minimization problem?
I think the error was just that in run_model_m I was passing a list as u0_val and it didn't have the same dimensions as m.time. So it should be u0_val=list_u1[0][0][0]

Apache Solr heatmap : How to convert in usable coordinate int2d array returned by heatmap

I'm using faceting heatmap on a spatial field which then returns a 2d array like this
"counts_ints2D",
[
null,
null,
null,
null,
[
0,
8,
4,
0,
0,
0,
0,
0,
0,
...
I want to locate those cluster on the map but the problem is that I don't know how to convert that 2d array in geo coordinates.
There's absolutely no documentation out there showing what to do with those integer.
Can somebody give some guidance ?
Going with the data you gave for Glasgow, and using the formula given in the comments, lets explore the coordinates in a python repl:
# setup
>>> minX = -180
>>> maxX = 180
>>> minY = -53.4375
>>> maxY = 74.53125
>>> columns = 256
>>> rows = 91
# calculate widths
>>> bucket_width = (maxX - minX) / columns
>>> bucket_width
1.40625
>>> bucket_height = (maxY - minY) / rows
>>> bucket_height
1.40625
# calculate area for bucket in heatmap facet for x = 124, y = 13
# point in lower left coordinate
>>> lower_left = {
... 'lat': maxY - (13 + 1) * bucket_height,
... 'lon': minX + 124 * bucket_width,
... }
>>> lower_left
{'lat': 54.84375, 'lon': -5.625}
# point in upper right
>>> upper_right = {
... 'lat': maxY - (13 + 1) * bucket_height + bucket_height,
... 'lon': minX + 124 * bucket_width + bucket_width,
... }
>>> upper_right
{'lat': 56.25, 'lon': -4.21875}
Let's graph these points on a map, courtesy of open street map. We generate a small CSV snippet we can import on umap (select the up arrow, choose 'csv' as the type and enter content into the text box). To our coordinates to show:
>>> bbox = [
... "lat,lon,description",
... str(lower_left['lat']) + "," + str(lower_left['lon']) + ",ll",
... str(upper_right['lat']) + "," + str(lower_left['lon']) + ",ul",
... str(upper_right['lat']) + "," + str(upper_right['lon']) + ",uu",
... str(lower_left['lat']) + "," + str(upper_right['lon']) + ",lu",
... ]
>>> print("\n".join(bbox))
lat,lon,description
54.84375,-5.625,ll
56.25,-5.625,ul
56.25,-4.21875,uu
54.84375,-4.21875,lu
After pasting these points into the import box creating the layer, we get this map:
Map based on Open Street Map data through uMap. This area encloses Glasgow as you expected.
Here's some code that takes 180th meridian (date line) wrapping into account:
$columns = $heatmap['columns'];
$rows = $heatmap['rows'];
$minX = $heatmap['minX'];
$maxX = $heatmap['maxX'];
$minY = $heatmap['minY'];
$maxY = $heatmap['maxY'];
$counts = $heatmap['counts_ints2D'];
// If our min longitude is greater than max longitude, we're crossing
// the 180th meridian (date line).
$crosses_meridian = $minX > $maxX;
// Bucket width needs to be calculated differently when crossing the
// meridian since it wraps.
$bucket_width = $crosses_meridian
? $bucket_width = (360 - abs($maxX - $minX)) / $columns
: $bucket_width = ($maxX - $minX) / $columns;
$bucket_height = ($maxY - $minY) / $rows;
$points = [];
foreach ($counts as $rowIndex => $row) {
if (!$row) continue;
foreach ($row as $columnIndex => $column) {
if (!$column) continue;
$point = []
$point['count'] = $column;
// Put the count in the middle of the bucket (adding a half height and width).
$point['lat'] = $maxY - (($rowIndex + 1) * $bucket_height) + ($bucket_height / 2);
$point['lng'] = $minX + ($columnIndex * $bucket_width) + ($bucket_width / 2);
// We crossed the meridian, so wrap back around to negative.
if ($point['lng'] > 180) {
$point['lng'] = -1 * (180 - ($point['lng'] % 180));
}
$points[] = $point;
}
}

Binary search to get original index in an Array

I have an array “a” like the following:
let a = [1.0, 2.0, 10.0, 0.0, 5.0] // original array
I am looking to find the number 10.0 in “a” using binary search.
For that I sort the array to get array “asr”:
let asr = a.sorted()
print(asr)
// [0.0, 1.0, 2.0, 5.0, 10.0]
Binary search for 10.0 in “asr” will return me index = 4. Whereas I am looking for index = 2 as in the original array “a”. And I am also looking for speed since my arrays are long.
Any suggestions?
I paste below the binary search algorithm I am using:
func binarySearch<T:Comparable>(inputArr:Array<T>, searchItem: T)->Int{
var lowerIndex = 0;
var upperIndex = inputArr.count - 1
while (true) {
let currentIndex = (lowerIndex + upperIndex)/2
if(inputArr[currentIndex] == searchItem) {
return currentIndex
} else if (lowerIndex > upperIndex) {
return -1
} else {
if (inputArr[currentIndex] > searchItem) {
upperIndex = currentIndex - 1
} else {
lowerIndex = currentIndex + 1
}
}
}
}
I give example of my x (time) and y (value) arrays. For multiple such arrays, I need to find maximum of values in y and store the related unique x value.
let x = [230.0, 231.0, 232.0, 233.0, 234.0, 235.0, 236.0, 237.0, 238.0, 239.0, 240.0, 241.0, 242.0, 243.0, 244.0, 245.0, 246.0, 247.0, 248.0, 249.0, 250.0, 251.0, 252.0, 253.0, 254.0, 255.0, 256.0, 257.0, 258.0, 259.0, 260.0, 261.0, 262.0, 263.0, 264.0, 265.0, 266.0, 267.0, 268.0, 269.0, 270.0, 271.0, 272.0, 273.0, 274.0, 275.0, 276.0, 277.0, 278.0, 279.0, 280.0, 281.0, 282.0, 283.0, 284.0, 285.0, 286.0, 287.0, 288.0, 289.0, 290.0, 291.0, 292.0, 293.0, 294.0, 295.0, 296.0, 297.0, 298.0, 299.0, 300.0, 301.0, 302.0, 303.0, 304.0, 305.0, 306.0, 307.0, 308.0, 309.0, 310.0, 311.0, 312.0, 313.0, 314.0, 315.0, 316.0, 317.0, 318.0, 319.0, 320.0, 321.0, 322.0, 323.0, 324.0, 325.0, 326.0, 327.0, 328.0, 329.0, 330.0, 331.0, 332.0, 333.0, 334.0, 335.0, 336.0, 337.0, 338.0, 339.0, 340.0, 341.0, 342.0, 343.0, 344.0] // unique ascending time stamps
let y = [-0.0050202642876176198, 0.022393410398194001, 0.049790254951834603, 0.077149678828730195, 0.104451119608423, 0.131674058448602, 0.15879803550636501, 0.185802665315146, 0.21266765210574901, 0.239372805059962, 0.26589805348529699, 0.29222346189943499, 0.31832924501308402, 0.34419578259991401, 0.36980363424246498, 0.39513355394291599, 0.42016650458771598, 0.444883672255248, 0.46926648035572899, 0.49329660359275201, 0.51695598173596602, 0.54022683319452802, 0.56309166838114799, 0.58553330285668204, 0.60753487024536401, 0.62907983491101405, 0.65015200438466503, 0.67073554153427395, 0.690814976467372, 0.71037521815772098, 0.72940156578721904, 0.74787971979453605, 0.765795792622184, 0.783136319153938, 0.79988826683476, 0.816039045465632, 0.83157651666592303, 0.84648900299618601, 0.86076529673452795, 0.87439466829995904, 0.88736687431637595, 0.89967216531114802, 0.91130129304248497, 0.92224551745010597, 0.93249661322397404, 0.94204687598616199, 0.95088912808120296, 0.95901672397057403, 0.96642355522725798, 0.97310405512663301, 0.979053202830236, 0.98426652715924901, 0.98874010995489703, 0.99247058902319396, 0.99545516066186102, 0.99769158176748995, 0.99917817152138799, 0.99991381265282298, 0.99989795227872502, 0.99913060231921702, 0.99761233948865402, 0.99534430486218395, 0.99232820301815805, 0.98856630075702201, 0.98406142539766805, 0.97881696265251605, 0.97283685408292797, 0.96612559413686105, 0.95868822677099297, 0.95053034165985795, 0.94165806999482904, 0.93207807987612601, 0.92179757130129403, 0.91082427075393002, 0.89916642539671898, 0.88683279687313799, 0.87383265472251104, 0.86017576941332496, 0.84587240500008198, 0.83093331140918203, 0.81536971635963695, 0.79919331692469797, 0.78241627074072795, 0.76505118686993401, 0.74711111632382299, 0.72860954225449404, 0.70956036982116599, 0.68997791573951905, 0.66987689752174295, 0.649272422415344, 0.62817997604904996, 0.60661541079433601, 0.584594933851312, 0.56213509506794002, 0.53925277450172504, 0.51596516973324402, 0.49228978294099801, 0.46824440774739501, 0.44384711584564701, 0.41911624341769299, 0.39407037735334999, 0.36872834128103499, 0.34310918142055002, 0.31723215226859403, 0.291116702127733, 0.26478245848970899, 0.23824921328407, 0.21153690800324301, 0.18466561871516801, 0.157655540974798, 0.130526974645817, 0.10330030864392201, 0.07599600561323, 0.048634586547227202, 0.0212366153658834] // y values (could be repeating)
It is natural to express a binary search as a recursive algorithm - and usually clearer, at least when you are comfortable with recursion. How about this:
func binarySearchHelper <T:Comparable> (array: Array<T>, item:T, lower:Int, upper:Int) -> Int? {
guard lower <= upper else { return nil }
let center = (lower + upper) / 2
return array[center] == item
? center
: ((lower == center ? nil : binarySearchHelper (array: array, item: item, lower: lower, upper: center)) ??
(upper == center + 1 ? nil : binarySearchHelper (array: array, item: item, lower: center + 1, upper: upper)))
}
func binarySearch <T:Comparable> (array: Array<T>, item:T) -> Int? {
return binarySearchHelper (array: array, item: item, lower: array.startIndex, upper: array.endIndex)
}
Use another array which keeps the indexes. Like:
let indexArray = [0, 1, 2, 3, 4]
Then whenever you switch a number in your original array, switch the equivalent values in indexArray.
At the end the index array would be like:
[3, 0, 1, 4, 2]
using this you can easily get the original index.
If you send the code you are using to sort I can change the code and show you how to do it..
Another method:
You can keep a copy of your original array:
let copyArray = a.copy()
then use this to find the index of each value:
let indexOfA = copyArray.index(of: "aValue")
copyArray[indexOfA] = nil
// OR if the values are all positive
copyArray[indexOfA] = -1

Array subscript out of bouds in an IF statement

Below is a section for my code.
The user inputs a value for Energy, if the value is on the array ARRDAT(1,x), then I want it to assign the related variables ARRDAT(2,x), ARRDAT(3,x), and ARRDAT(4,x).
If the data is not on the array, I will interpolate (note: only values between two of my given points will be inputted).
I am able to compile it with a warning
warning 179 - Comparning floating point quantities for equality may give misleading results
and as soon as I input any value, whether it's in the table or not I get
error 11, array subscript(s) out of bounds
The error occurs on the line
IF (ARRDAT(1,x) == Energy) THEN
I haven't been able to fix it.
IMPLICIT NONE
REAL :: Dose, Energy, Source, FeAtt, PbAtt, Resp, Distance, Mu, Thickness
INTEGER :: x, y, upp, low
CHARACTER(LEN=4) :: Material
REAL, PARAMETER :: PI = 3.1415926
real, dimension(1:4,1:25) :: ARRDAT
!Energy MeV
ARRDAT(1,1) = 0.01
ARRDAT(1,2) = 0.015
ARRDAT(1,3) = 0.02
ARRDAT(1,4) = 0.03
ARRDAT(1,5) = 0.04
ARRDAT(1,6) = 0.05
ARRDAT(1,7) = 0.06
ARRDAT(1,8) = 0.08
ARRDAT(1,9) = 0.10
ARRDAT(1,10) = 0.15
ARRDAT(1,11) = 0.20
ARRDAT(1,12) = 0.30
ARRDAT(1,13) = 0.40
ARRDAT(1,14) = 0.50
ARRDAT(1,15) = 0.60
ARRDAT(1,16) = 0.80
ARRDAT(1,17) = 1.0
ARRDAT(1,18) = 1.5
ARRDAT(1,19) = 2.0
ARRDAT(1,20) = 3.0
ARRDAT(1,21) = 4.0
ARRDAT(1,22) = 5.0
ARRDAT(1,23) = 6.0
ARRDAT(1,24) = 8.0
ARRDAT(1,25) = 10.0
!Response Function Sv*cm2
ARRDAT(2,1) = 0.062
ARRDAT(2,2) = 0.157
ARRDAT(2,3) = 0.238
ARRDAT(2,4) = 0.329
ARRDAT(2,5) = 0.365
ARRDAT(2,6) = 0.384
ARRDAT(2,7) = 0.400
ARRDAT(2,8) = 0.451
ARRDAT(2,9) = 0.533
ARRDAT(2,10) = 0.777
ARRDAT(2,11) = 1.03
ARRDAT(2,12) = 1.56
ARRDAT(2,13) = 2.06
ARRDAT(2,14) = 2.54
ARRDAT(2,15) = 2.99
ARRDAT(2,16) = 3.83
ARRDAT(2,17) = 4.60
ARRDAT(2,18) = 6.24
ARRDAT(2,19) = 7.66
ARRDAT(2,20) = 10.2
ARRDAT(2,21) = 12.5
ARRDAT(2,22) = 14.7
ARRDAT(2,23) = 16.7
ARRDAT(2,24) = 20.8
ARRDAT(2,25) = 24.7
!mu/rho for iron cm2/g
ARRDAT(3,1) = 169.4
ARRDAT(3,2) = 56.33
ARRDAT(3,3) = 25.16
ARRDAT(3,4) = 7.891
ARRDAT(3,5) = 3.450
ARRDAT(3,6) = 1.833
ARRDAT(3,7) = 1.113
ARRDAT(3,8) = 0.5391
ARRDAT(3,9) = 0.3340
ARRDAT(3,10) = 0.1786
ARRDAT(3,11) = 0.1357
ARRDAT(3,12) = 0.1051
ARRDAT(3,13) = 0.09131
ARRDAT(3,14) = 0.08241
ARRDAT(3,15) = 0.07583
ARRDAT(3,16) = 0.06631
ARRDAT(3,17) = 0.05951
ARRDAT(3,18) = 0.04863
ARRDAT(3,19) = 0.04254
ARRDAT(3,20) = 0.03616
ARRDAT(3,21) = 0.03309
ARRDAT(3,22) = 0.03144
ARRDAT(3,23) = 0.03056
ARRDAT(3,24) = 0.02991
ARRDAT(3,25) = 0.02994
!mu/rho for lead cm2/g
ARRDAT(4,1) = 125.7
ARRDAT(4,2) = 108.3
ARRDAT(4,3) = 84.02
ARRDAT(4,4) = 28.94
ARRDAT(4,5) = 13.44
ARRDAT(4,6) = 7.386
ARRDAT(4,7) = 4.531
ARRDAT(4,8) = 2.112
ARRDAT(4,9) = 5.337
ARRDAT(4,10) = 1.910
ARRDAT(4,11) = 0.9359
ARRDAT(4,12) = 0.3732
ARRDAT(4,13) = 0.2148
ARRDAT(4,14) = 0.1499
ARRDAT(4,15) = 0.1167
ARRDAT(4,16) = 0.08408
ARRDAT(4,17) = 0.06803
ARRDAT(4,18) = 0.05087
ARRDAT(4,19) = 0.04530
ARRDAT(4,20) = 0.04200
ARRDAT(4,21) = 0.04178
ARRDAT(4,22) = 0.04260
ARRDAT(4,23) = 0.04382
ARRDAT(4,24) = 0.04670
ARRDAT(4,25) = 0.04969
WRITE(*,*) 'Please input the particle energy in MeV'
READ(*,*) Energy
x=0
DO x = 0, 25, 1
IF (ARRDAT(1,x) == Energy) THEN !if the data is already on our table
ARRDAT(2,x) = Resp
ARRDAT(3,x) = FeAtt
ARRDAT(4,x) = PbAtt
WRITE(*,*) 'CHECK 1'
ELSE IF (ARRDAT(1,x) < Energy) THEN !if the data is between two points
upp = x
low = x - 1
Energy = ((ARRDAT(1,upp) + ARRDAT(1,low))/2)
WRITE(*,*) Energy
ELSE
WRITE(*,*) 'Invalid Entry'
END IF
END DO
You declare your array to be 1-indexed with real, dimension(1:4,1:25) :: ARRDAT, but your DO loop begins with x = 0, so the first iteration of the loop looks for ARRDAT(1,0) == Energy, which is out of bounds. Start your DO loop with x = 1 and you should be fine.
Side note: Fortran automatically indexes arrays beginning with 1, so you can declare your array as real, dimension(4,25) :: ARRDAT, and it will have the same effect.
EDIT: As mentioned by #user5713492 in the comments, this code will still crash if the input Energy is greater than ARRDAT(1,1). You will either have to fix the lower bound of the linear interpolation or skip the interpolation entirely in the first iteration of the loop. For example:
DO x = 1, 25
IF (ARRDAT(1,x) == Energy) THEN
! ...
ELSE IF (ARRDAT(1,x) < Energy) THEN
upp = x
low = x - 1
IF (low < 1) low = 1 ! <-- make sure we don't go out of bounds on the array
! ...
END IF
END DO
... or ...
DO x = 1, 25
IF (ARRDAT(1,x) == Energy) THEN
! ...
ELSE IF (x > 1 .AND. ARRDAT(1,x) < Energy) THEN ! <-- don't interpolate for the first iteration
! ...
END IF
END DO
Now, about that warning concerning comparison of floating point numbers for equality: please read this and try to avoid ==, >=, and <= operators when comparing reals.

Resources