Jump to content

Element-wise operations: Difference between revisions

no edit summary
m (→‎{{header|Perl 6}}: Combine into a single file for ease of testing)
No edit summary
Line 469:
}
}</lang>
 
=={{header|Fortran}}==
 
All element based operations are suported by default in Fortran(90+)
 
<lang Fortran>
program element_operations
implicit none
 
real(kind=4), dimension(3,3) :: a,b
integer :: i
 
a=reshape([(i,i=1,9)],shape(a))
 
print*,'addition'
b=a+a
call print_arr(b)
 
print*,'multiplication'
b=a*a
call print_arr(b)
 
print*,'division'
b=a/b
call print_arr(b)
 
print*,'exponentiation'
b=a**a
call print_arr(b)
 
print*,'trignometric'
b=cos(a)
call print_arr(b)
 
print*,'mod'
b=mod(int(a),3)
call print_arr(b)
 
print*,'element selection'
b=0
where(a>3) b=1
call print_arr(b)
 
print*,'elemental functions can be applied to single values:'
print*,square(3.0)
print*,'or element wise to arrays:'
b=square(a)
call print_arr(b)
 
 
contains
 
elemental real function square(a)
real, intent(in) :: a
square=a*a
end function square
 
subroutine print_arr(arr)
real, intent(in) :: arr(:,:)
integer :: i
do i=1,size(arr,dim=2)
print*,arr(:,i)
end do
end subroutine print_arr
end program element_operations
</lang>
 
{{out}}
<pre>
addition
2.00000000 4.00000000 6.00000000
8.00000000 10.0000000 12.0000000
14.0000000 16.0000000 18.0000000
multiplication
1.00000000 4.00000000 9.00000000
16.0000000 25.0000000 36.0000000
49.0000000 64.0000000 81.0000000
division
1.00000000 0.500000000 0.333333343
0.250000000 0.200000003 0.166666672
0.142857149 0.125000000 0.111111112
exponentiation
1.00000000 4.00000000 27.0000000
256.000000 3125.00000 46656.0000
823543.000 16777216.0 387420480.
trignometric
0.540302277 -0.416146845 -0.989992499
-0.653643608 0.283662200 0.960170269
0.753902256 -0.145500034 -0.911130250
mod
1.00000000 2.00000000 0.00000000
1.00000000 2.00000000 0.00000000
1.00000000 2.00000000 0.00000000
element selection
0.00000000 0.00000000 0.00000000
1.00000000 1.00000000 1.00000000
1.00000000 1.00000000 1.00000000
elemental functions can be applied to single values:
9.00000000
or element wise to arrays:
1.00000000 4.00000000 9.00000000
16.0000000 25.0000000 36.0000000
49.0000000 64.0000000 81.0000000
 
</pre>
 
=={{header|Go}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.