Fortran中的非表格数据结构
我想为非表格数据构建一个数据结构。我不确定用(现代)Fortran做这件事的正确方法是什么 我有一套房子的数据,包括它们的位置(纬度、经度)和价格。我还有一个工厂的数据,包括它们的位置(纬度、经度)和它们产生的污染量。对于每个房子,我需要创建一个在房子5公里半径范围内的工厂列表。不仅仅是这些工厂的数量,还有这些工厂的全部(纬度、经度、污染)媒介。每家房子附近都有不同数量的工厂,从零到大约八十家不等Fortran中的非表格数据结构,fortran,fortran90,Fortran,Fortran90,我想为非表格数据构建一个数据结构。我不确定用(现代)Fortran做这件事的正确方法是什么 我有一套房子的数据,包括它们的位置(纬度、经度)和价格。我还有一个工厂的数据,包括它们的位置(纬度、经度)和它们产生的污染量。对于每个房子,我需要创建一个在房子5公里半径范围内的工厂列表。不仅仅是这些工厂的数量,还有这些工厂的全部(纬度、经度、污染)媒介。每家房子附近都有不同数量的工厂,从零到大约八十家不等 MODULE someDefinitions IMPLICIT NONE INTEGER, PAR
MODULE someDefinitions
IMPLICIT NONE
INTEGER, PARAMETER :: N_houses=82390, N_factories=4215
TYPE house
REAL :: lat,lon,price
! a few more fields which are not important here
END TYPE
TYPE factory
REAL :: lat,lon,pollution
! a few more fields which are not important here
END TYPE
Contains
PURE FUNCTION haversine(deglat1,deglon1,deglat2,deglon2) RESULT (dist)
! Some code for computing haversine distance in meters
END FUNCTION haversine
END MODULE someDefinitions
PROGRAM createStructure
USE someDefinitions
IMPLICIT NONE
TYPE(factory), DIMENSION(N_factories) :: factories
TYPE(house), DIMENSION(N_houses) :: houses
INTEGER :: i,j
! more variables definitions as needed
! code to read houses data from the disk
! code to read factories data from the disk
DO i=1,N_houses
DO j=1,N_factories
!here I compute the distance between houses(i) and factories(j)
! If this distance<=5000 I want to add the index j to the list of indices
! associated with house i. How? What is the right data structure to do
! that? some houses have zero factories within 5000 meters from them.
! Some houses have about 80 factories around them. It's unbalanced.
END DO !j
END DO !i
END PROGRAM createStructure
模块定义
隐式无
整数,参数::N_houses=82390,N_factories=4215
样板房
雷亚尔:拉特,朗,普莱斯
! 这里还有几个不重要的领域
端型
类型工厂
雷亚尔:纬度、经度、污染
! 这里还有几个不重要的领域
端型
包含
纯函数哈弗森(deglat1,deglon1,deglat2,deglon2)结果(dist)
! 用米计算哈弗线距离的一些代码
端功能哈弗森
结束模块定义
程序结构
使用一些定义
隐式无
类型(工厂)、尺寸(N_工厂)::工厂
类型(房屋),尺寸(N_房屋)::房屋
整数::i,j
! 根据需要提供更多变量定义
! 从磁盘读取数据的代码
! 从磁盘读取工厂数据的代码
i=1,N_房屋吗
j=1,N_工厂
!这里我计算房屋(I)和工厂(j)之间的距离
! 如果这个距离,使用太多嵌套的派生类型可能会变得单调乏味。下面是一个使用2D数组处理除所需列表之外的所有数据的示例。这类似于简单实现的K-最近邻(KNN)算法。当然,可能会有更好的算法,但以下是一个良好的开端
program NoStrucyures
implicit none
type listi
real, allocatable :: item(:,:)
end type
integer, parameter :: N_houses=82390, N_factories=4215
real :: houses(N_houses,3)
real :: factories(N_factories,3)
real :: distance(N_factories)
type(listi) :: list(N_houses)
integer :: i, j, k, within5k
! Generating dummy data
call random_number(houses)
call random_number(factories)
houses = houses * 500000
factories = factories * 500000
do i = 1, N_houses
distance = sqrt((houses(i,1)-factories(:,1))**2 + (houses(i,2)-factories(:,2))**2)
within5k = count( distance <= 5000 )
if (within5k > 0) then
allocate(list(i)%item(within5k,3))
k = 0
do j = 1, N_factories
if (distance(j) <= 5000) then
k = k + 1
list(i)%item(k,:) = factories(j,:)
end if
end do
else
list(i)%item = reshape([-1, -1, -1],[1,3])
end if
end do
do i=1,10
print *, list(i)%item
end do
end program NoStrucyures
程序NoStrucyures
隐式无
listi型
实型,可分配::项(:,:)
端型
整数,参数::N_houses=82390,N_factories=4215
房地产::房屋(N_房屋,3)
雷亚尔:工厂(N_工厂,3)
实际::距离(N_工厂)
类型(列表i)::列表(N_房屋)
整数::i,j,k,带5k
! 生成虚拟数据
随机呼叫号码(房屋)
随机电话号码(工厂)
房屋=房屋*500000
工厂=工厂*500000
i=1,N_房屋吗
距离=平方米((房屋(i,1)-工厂(:,1))**2+(房屋(i,2)-工厂(:,2))**2)
在5K内=计数(距离0),然后
分配(列出(i)%项目(不超过5K,3))
k=0
j=1,N_工厂
if(distance(j)使用过多的嵌套派生类型可能会变得单调乏味。下面是一个示例,使用2D数组处理除所需列表之外的所有数据。这类似于天真地实现的K-最近邻(KNN)算法。当然,可能有更好的算法,但以下可能是一个良好的开端
program NoStrucyures
implicit none
type listi
real, allocatable :: item(:,:)
end type
integer, parameter :: N_houses=82390, N_factories=4215
real :: houses(N_houses,3)
real :: factories(N_factories,3)
real :: distance(N_factories)
type(listi) :: list(N_houses)
integer :: i, j, k, within5k
! Generating dummy data
call random_number(houses)
call random_number(factories)
houses = houses * 500000
factories = factories * 500000
do i = 1, N_houses
distance = sqrt((houses(i,1)-factories(:,1))**2 + (houses(i,2)-factories(:,2))**2)
within5k = count( distance <= 5000 )
if (within5k > 0) then
allocate(list(i)%item(within5k,3))
k = 0
do j = 1, N_factories
if (distance(j) <= 5000) then
k = k + 1
list(i)%item(k,:) = factories(j,:)
end if
end do
else
list(i)%item = reshape([-1, -1, -1],[1,3])
end if
end do
do i=1,10
print *, list(i)%item
end do
end program NoStrucyures
程序NoStrucyures
隐式无
listi型
实型,可分配::项(:,:)
端型
整数,参数::N_houses=82390,N_factories=4215
房地产::房屋(N_房屋,3)
雷亚尔:工厂(N_工厂,3)
实际::距离(N_工厂)
类型(列表i)::列表(N_房屋)
整数::i,j,k,带5k
!正在生成虚拟数据
随机呼叫号码(房屋)
随机电话号码(工厂)
房屋=房屋*500000
工厂=工厂*500000
i=1,N_房屋吗
距离=平方米((房屋(i,1)-工厂(:,1))**2+(房屋(i,2)-工厂(:,2))**2)
在5K内=计数(距离0),然后
分配(列出(i)%项目(不超过5K,3))
k=0
j=1,N_工厂
if(距离(j)字典呢?看。可能有更好的算法方法来处理你的问题,但乍一看,你可能对的方法感兴趣。字典呢?看。可能有更好的算法方法来处理你的问题,但乍一看,你可能对的方法感兴趣。