|
|
 |
 |
 |
 |
Fortran Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
Type assign initial values?
My program: .... type :: Typ_1 real(8) :: a = 100. real(8) :: t ..... end type Typ_1 .... type(Typ_1) :: Typ1 Typ1 = Typ_1(100.,200.) I want to ask that weather I can assign partial values of the Typ1 because I have already assgin the initial value(like a = 100.). E.g. Typ1 = Typ_1(t = 200.) or use other format???
On Jun 4, 2:00 pm, li.sim@gmail.com wrote:
> My program: > .... > type :: Typ_1 > real(8) :: a = 100. > real(8) :: t > ..... > end type Typ_1 > .... > type(Typ_1) :: Typ1 > Typ1 = Typ_1(100.,200.) > I want to ask that weather I can assign partial values of the Typ1 > because I have already assgin the initial value(like a = 100.). E.g. > Typ1 = Typ_1(t = 200.) or use other format???
This is a F2003-style structure constructor. It is already implemented in g95, for example. (www.g95.org)
On Jun 4, 7:00 am, li.sim@gmail.com wrote:
> My program: > .... > type :: Typ_1 > real(8) :: a = 100. > real(8) :: t > ..... > end type Typ_1 > .... > type(Typ_1) :: Typ1 > Typ1 = Typ_1(100.,200.) > I want to ask that weather I can assign partial values of the Typ1 > because I have already assgin the initial value(like a = 100.). E.g. > Typ1 = Typ_1(t = 200.) or use other format???
A structure constructor expression list must agree in number and order with the components of the derived type. Thus, to assign a single value you need the reference form as Typ1%t = 200. --
On Jun 4, 6:00 am, li.sim@gmail.com wrote: > type :: Typ_1 > real(8) :: a = 100. > real(8) :: t > ..... > end type Typ_1
"real(8)" is non-portable, so you might want to try: integer, parameter :: rdp = kind(1.D0) type :: Typ_1 real(rdp) :: a = 100._rdp, t end type Typ_1 > I want to ask that weather I can assign partial values of the Typ1 > because I have already assgin the initial value(like a = 100.). E.g. > Typ1 = Typ_1(t = 200.) or use other format???
What about "Typ1%t = 200._rdp"?
The really mean of I is that you have to one by one(like Typ1%t = 200.)define many..many..many(like a1,a2,a3,). I want to known that if I assin Typ1 in one command,the a1,a2,a3 must be reassigned??? This is very bad! type :: Typ_1 real(8) :: a1 = 100. real(8) :: a2 = 102. real(8) :: a3 = 103. real(8) :: a4 ..... ..... ..... ..... real(8) :: a10 end type Typ_1 type(Typ_1) :: Typ1 Typ1 = Typ_1(100.,102.,103.,104.....)
<li.sim @gmail.com> wrote in message news:1180968119.632996.216360@g37g2000prf.googlegroups.com... > The really mean of I is that you have to one by one(like Typ1%t = > 200.)define many..many..many(like a1,a2,a3,). I want to known that if > I assin Typ1 in one command,the a1,a2,a3 must be reassigned??? This is > very bad!
... and why it was changed for f2003. I understand that Cray and g95 already implement the new style. Regards, Mike Metcalf
Michael Metcalf <michaelmetc @compuserve.com> wrote: > <li.sim @gmail.com> wrote in message > news:1180968119.632996.216360@g37g2000prf.googlegroups.com... > > The really mean of I is that you have to one by one(like Typ1%t = > > 200.)define many..many..many(like a1,a2,a3,). I want to known that if > > I assin Typ1 in one command,the a1,a2,a3 must be reassigned??? This is > > very bad! > ... and why it was changed for f2003. I understand that Cray and g95 already > implement the new style.
As an aside, do note (for the OP; I know Michael understands) that this has nothing in particular to do with assignment. It is just a question of structure constructor syntax, irrelevant to whether the constructor is used in an assignment statement or some other context. -- Richard Maine | Good judgement comes from experience; email: last name at domain . net | experience comes from bad judgement. domain: summertriangle | -- Mark Twain
li.sim @gmail.com wrote: > The really mean of I is that you have to one by one(like Typ1%t = > 200.)define many..many..many(like a1,a2,a3,). I want to known that if > I assin Typ1 in one command,the a1,a2,a3 must be reassigned??? This is > very bad! I am not so sure what you are asking. I believe that Fortran, even Fortran 2003, does not allow scalar to structure assignment. Initialization is different from assignment, but I believe it is also not allowed there. For large structures that might mean a lot of assignment statements or initializations. For comparison purposes note that PL/I does allow scalar to structure assignment similar to scalar to array assignment. PL/I also allows scalar/structure expressions, for example A=A+1; would add one to each element of structure A. > type :: Typ_1 > real(8) :: a1 = 100. > real(8) :: a2 = 102. > real(8) :: a3 = 103. > real(8) :: a4 > ..... > ..... > ..... > ..... > real(8) :: a10 > end type Typ_1 > type(Typ_1) :: Typ1 > Typ1 = Typ_1(100.,102.,103.,104.....)
This example is confusing. Be sure that you know the difference between initialization and assignment, especially regarding the SAVE attribute. -- glen
<li.sim @gmail.com> wrote in message news:1180968119.632996.216360@g37g2000prf.googlegroups.com... > Typ1 = Typ_1(100.,102.,103.,104.....)
Fortran 95 permits the syntax: Typ1 = Typ_1_construct(a4 = 104.0) -- write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, & 6.0134700243160014d-154/),(/'x'/)); end
James Van Buskirk <not_va@comcast.net> wrote: No. That's an f2003 feature - not f95. I had to go check to make sure I had it right because I've been known to confuse such things. Apparently I'm not the only one. :-) -- Richard Maine | Good judgement comes from experience; email: last name at domain . net | experience comes from bad judgement. domain: summertriangle | -- Mark Twain
"Richard Maine" <nos @see.signature> wrote in message news:1hz6foy.1k1jqe4bxfembN%nospam@see.signature... > No. That's an f2003 feature - not f95. > I had to go check to make sure I had it right because I've been known to > confuse such things. Apparently I'm not the only one. :-)
Sigh. I send you back to check again. ! File: udt_assign.f90 ! Public domain 2007 James Van Buskirk module mykinds implicit none integer, parameter :: sp = kind(1.0) integer, parameter :: dp = kind(1.0d0) end module mykinds module Typ1_mod use mykinds implicit none type Typ_1 real(dp) :: a1 = 100.0_sp real(dp) :: a2 = 102.0_sp real(dp) :: a3 = 103.0_sp real(dp) :: a4 real(dp) :: a5 real(dp) :: a6 real(dp) :: a7 real(dp) :: a8 real(dp) :: a9 real(dp) :: a10 end type Typ_1 interface assignment(=) module procedure Typ_1_assign end interface assignment(=) contains elemental function Typ_1_construct(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) type(Typ_1) Typ_1_construct real(sp), intent(in), optional :: a1 real(sp), intent(in), optional :: a2 real(sp), intent(in), optional :: a3 real(sp), intent(in), optional :: a4 real(sp), intent(in), optional :: a5 real(sp), intent(in), optional :: a6 real(sp), intent(in), optional :: a7 real(sp), intent(in), optional :: a8 real(sp), intent(in), optional :: a9 real(sp), intent(in), optional :: a10 if(present(a1)) then Typ_1_construct%a1 = a1 end if if(present(a2)) then Typ_1_construct%a2 = a2 end if if(present(a3)) then Typ_1_construct%a3 = a3 end if if(present(a4)) then Typ_1_construct%a4 = a4 else Typ_1_construct%a4 = 0 end if if(present(a5)) then Typ_1_construct%a5 = a5 else Typ_1_construct%a5 = 0 end if if(present(a6)) then Typ_1_construct%a6 = a6 else Typ_1_construct%a6 = 0 end if if(present(a7)) then Typ_1_construct%a7 = a7 else Typ_1_construct%a7 = 0 end if if(present(a8)) then Typ_1_construct%a8 = a8 else Typ_1_construct%a8 = 0 end if if(present(a9)) then Typ_1_construct%a9 = a9 else Typ_1_construct%a9 = 0 end if if(present(a10)) then Typ_1_construct%a10 = a10 else Typ_1_construct%a10 = 0 end if end function Typ_1_construct elemental subroutine Typ_1_assign(output, input) type(Typ_1), intent(out) :: output type(Typ_1), intent(in) :: input output%a1 = input%a1 output%a2 = input%a2 output%a3 = input%a3 output%a4 = input%a4 output%a5 = input%a5 output%a6 = input%a6 output%a7 = input%a7 output%a8 = input%a8 output%a9 = input%a9 output%a10 = input%a10 end subroutine Typ_1_assign end module Typ1_mod program test use mykinds use Typ1_mod implicit none type(Typ_1) Typ1 Typ1 = Typ_1_construct(a4 = 104.0) write(*,*) Typ1 end program test ! End of file: udt_assign.f90 Output with ifort: 100.000000000000 102.000000000000 103.000000000000 104.000000000000 0.000000000000000E+000 0.000000000000000E+000 0.000000000000000E+000 0.000000000000000E+000 0.000000000000000E+000 0.000000000000000E+000 -- write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, & 6.0134700243160014d-154/),(/'x'/)); end
James Van Buskirk <not_va@comcast.net> wrote: > "Richard Maine" <nos @see.signature> wrote in message > news:1hz6foy.1k1jqe4bxfembN%nospam@see.signature... > > No. That's an f2003 feature - not f95. > > I had to go check to make sure I had it right because I've been known to > > confuse such things. Apparently I'm not the only one. :-) > Sigh. I send you back to check again.
[sample code elided] Oh. I see. You aren't actually talking about a structure constructor. But then I guess you didn't say that you were. You are talking about a reference to a user-written function. I missed the fact that the function had a diferent name from the type, which should have clued me in. If f2003, this (with the type name) is directly the syntax of a constructor. -- Richard Maine | Good judgement comes from experience; email: last name at domain . net | experience comes from bad judgement. domain: summertriangle | -- Mark Twain
|
 |
 |
 |
 |
|