使用IntStream.range的Java和部分列表的嵌套属性

时间:2019-11-29 03:44:49

标签: java lambda java-8 sum java-stream

我有这个基本代码(列表的大小大于2)。

# imports
import numpy as np
import scipy.sparse as sp
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt

# grid sizing
R=0.05  #sphere radius
N=1000#number of points
D=0.00002 #diffusion coefficient
k=10  # Arrhenius 
Cs=1.0 # Boundary concentration
C0=0.0 # Initial concentration
time_constant=R**2.0/D

dr=R/(N-1)

# Algebra simplification
a=D/dr**2

Init_conc=np.linspace(0,0,N)
B=np.zeros(N)
B[N-1]=Cs*(a+a/(N-1))
#
e1 = np.ones(N)
e2 = np.ones(N)
e3 = np.ones(N)
#
#
#
e1[0]=-k-6*a
e1[1:]=-k-2*a
# 
#
e2[1]=6*a
for i in range(2,N) :
 e2[i]=a+a/(i-1)
#
#
#
for i in range (0,N-1) :
 e3[i]=a-a/(i+1)


A = sp.spdiags([e3,e1,e2],[-1,0,1],N,N,format="csc")


def dc_dt(t,C)   :
    dc=A.dot(C)+B
    return dc

# Solving the system, I want to implement the same thing with vectorized='True'
OutputTimes=np.linspace(0,0.2*time_constant,100)
ans=solve_ivp(dc_dt,(0,0.2*time_constant),Init_conc,method='RK45',t_eval=OutputTimes,vectorized='False')
print (ans)

是否可以用int delayedSamples = 0; for (int i = 0; i < list.size() - 2; i++) { delayedSamples += list.get(i).getChunk().length / size; } 替换之前的代码?喜欢:

IntStream.range

有lambda替代品吗?

1 个答案:

答案 0 :(得分:3)

您可以使用:

int delayedSamples = list.stream()
                         .map(item -> item.getChunk())
                         .limit(list.size() - 2)
                         .collect(Collectors.summingInt(chunk -> chunk.length / size));