lets say I have this number here 100675
how to turn it into 101000
all the solution I have found on google are solving decimals number.
The bash shell is able to do calculations internally, such as with the following transcript:
pax:~> for x in 100675 100499 100500 100999 101000; do
...:~> ((y = (x + 500) / 1000 * 1000))
...:~> echo " $x becomes $y"
...:~> done
100675 becomes 101000
100499 becomes 100000
100500 becomes 101000
100999 becomes 101000
101000 becomes 101000
This statement, ((y = (x + 500) / 1000 * 1000)), first adds 500 to make the otherwise-truncating integer division by 1,000 into a rounding division, then re-multiplies it by 1,000.
This is a bit of a weird function, but here goes a rough version that might be what you need. At least this could be a starting point.
# in:
# $1 - the number to round
# $2 - the 10 power to round at. Defaults to 3 (i.e. 1000)
# output:
# The rounded number
roundPow()
{
local n="$1"
local pow="${2:-3}"
local div="$((10 ** pow))"
echo "$((((n + div / 2) / div) * div))"
}
This is very rough around the edges, it's not validating parameters, etc, but should give you a baseline.
Hope this helps.
I am implementing a Szudik's pairing function in Matlab, where i pair 2 values coming from 2 different matrices X and Y, into a unique value given by the function 'CantorPairing2D(X,Y), After this i reverse the process to check for it's invertibility given by the function 'InverseCantorPairing2( X )'. But I seem to get an unusual problem, when i check this function for small matrices of size say 10*10, it works fine, but the for my code i have to use a 256 *256 matrices A and B, and then the code goes wrong, actually what it gives is a bit strange, because when i invert the process, the values in the matrix A, are same as cvalues of B in some places, for instance A(1,1)=B(1,1), and A(1,2)=B(1,2). Can somebody help.
VRNEW=CantorPairing2D(VRPRO,BLOCK3);
function [ Z ] = CantorPairing2D( X,Y )
[a,~] =(size(X));
Z=zeros(a,a);
for i=1:a
for j=1:a
if( X(i,j)~= (max(X(i,j),Y(i,j))) )
Z(i,j)= X(i,j)+(Y(i,j))^2;
else
Z(i,j)= (X(i,j))^2+X(i,j)+Y(i,j);
end
end
end
Z=Z./1000;
end
function [ A,B ] = InverseCantorPairing2( X )
[a, ~] =(size(X));
Rfinal=X.*1000;
A=zeros(a,a);
B=zeros(a,a);
for i=1:a
for j=1:a
if( ( Rfinal(i,j)- (floor( sqrt(Rfinal(i,j))))^2) < floor(sqrt(Rfinal(i,j))) )
T=floor(sqrt(Rfinal(i,j)));
B(i,j)=T;
A(i,j)=Rfinal(i,j)-T^2;
else
T=floor( (-1+sqrt(1+4*Rfinal(i,j)))/2 );
A(i,j)=T;
B(i,j)=Rfinal(i,j)-T^2-T;
end
end
end
end
Example if A= 45 16 7 17
7 22 11 25
11 12 9 17
2 11 3 5
B= 0 0 0 1
0 0 0 1
1 1 1 1
1 3 0 0
Then after pairing i get
C =2.0700 0.2720 0.0560 0.3070
1.4060 0.5060 0.1320 0.6510
0.1330 0.1570 0.0910 0.3070
0.0070 0.1350 0.0120 0.0300
after the inverse pairing i should get the same A and same B. But for bigger matrices it is giving unusual behaviour, because some elements of A are same as B.
If possible it would help immensely a counter example where your code does fail.
I got to reproduce your code behaviour and I have rewritten your code in a vectorised fashion. You should get the bug, but hopefully it is a first step to uncover the underlying logic and find the bug itself.
I am not familiar with the specific algorithm, but I observe a discrepancy in the CantorPairing definition.
for elements where Y = X your if statement would be false, since X = max(X,X); so for those elements your Z would be X^2+X+Y, but for hypothesis X =Y, therefore your would have:
X^2+X+X = X^2+2*X;
now, if we perturb slightly the equation and suppose Y = X + 10*eps, your if statement would be true (since Y > X) and your Z would be X + Y ^2; since X ~=Y we can approximate to X + X^2
therefore your equation is very temperamental to numerical approximation ( and you definitely have a discontinuity in Z). Again, I am not familiar with the algorithm and it may very well be the behaviour you want, but it is unlikely: so I am pointing this out.
Following is my version of your code, I report it also because I hope it will be pedagogical in getting you acquainted with logical indexing and vectorized code (which is the idiomatic form for MATLAB, let alone much faster than nested for loops).
function [ Z ] = CantorPairing2D( X,Y )
[a,~] =(size(X));
Z=zeros(a,a);
firstConditionIndeces = Y > X; % if Y > X then X is not the max between Y and X
% update elements on which to apply first equation
Z(firstConditionIndeces) = X(firstConditionIndeces) + Y(firstConditionIndeces).^2;
% update elements on the remaining elements
Z(~firstConditionIndeces) = X(~firstConditionIndeces).^2 + X(~firstConditionIndeces) + Y(~firstConditionIndeces) ;
Z=Z./1000;
end
function [ A,B ] = InverseCantorPairing2( X )
[a, ~] =(size(X));
Rfinal=X.*1000;
A=zeros(a,a);
B=zeros(a,a);
T = zeros(a,a) ;
% condition deciding which updates to be applied
indecesToWhichApplyFstFcn = Rfinal- (floor( sqrt(Rfinal )))^2 < floor(sqrt(Rfinal)) ;
% elements on which to apply the first update
T(indecesToWhichApplyFstFcn) = floor(sqrt(Rfinal )) ;
B(indecesToWhichApplyFstFcn) = floor(Rfinal(indecesToWhichApplyFstFcn)) ;
A(indecesToWhichApplyFstFcn) = Rfinal(indecesToWhichApplyFstFcn) - T(indecesToWhichApplyFstFcn).^2;
% updates on which to apply the remaining elements
A(~indecesToWhichApplyFstFcn) = floor( (-1+sqrt(1+4*Rfinal(~indecesToWhichApplyFstFcn )))/2 ) ;
B(~indecesToWhichApplyFstFcn) = Rfinal(~indecesToWhichApplyFstFcn) - T(~indecesToWhichApplyFstFcn).^2 - T(~indecesToWhichApplyFstFcn) ;
end
I would like to save calculation time of turtle movement (question posted here: NetLogo: how to make the calculation of turtle movement easier?). In original move-turtles procedure authors use many "let" - local variables. I suppose that I can easily replace these "let" variables with built-in NetLogo primitives p.ex. here:
; original code with "let" local variables
let np patches in-radius 15 ; define your perceptual range
let bnp max-one-of np [totalattract] ; max of [totalattract] of patches in your neighborhood
let ah [totalattract] of patch-here ; [totalattract] of my patch
let xcorhere [pxcor] of patch-here
let ycorhere [pycor] of patch-here
let abnp [totalattract] of bnp
ifelse abnp - ah > 2 [ ...
can be replaced by this condition?
; make the same condition with NetLogo primitives
ifelse ([totalattract] of max-one-of patches in-radius 15 [totalattract] - [totalattract] of patch-here > 2 [ ...
Please, will utilization of "let" local variables save computational time or will it be more time consuming? How can I easily verify it? Thank you for your time !
(PS: Following comments to my previous question I suppose that primitives variables will be more efficient, I just prefer to be more sure)
The difference is in the number of times each reporter is being calculated. If you say let np patches in-radius 15 then that actually calculates the number of patches within 15 distance and gives that value to the variable named np. Using np in calculations directly substitutes the value that is saved. If you have to use it 10 times in your code, then using the let means it is calculated once and simply read 10 times. Alternatively, if you don't store it in a variable, then you will need patches in-radius 15 at 10 different places in the code and, EACH TIME, NetLogo will need to calculate this value.
Apparently is looks like local variables within [] works faster then primitives NetLogo variables.
Comparing 1) only NL primitives
let flightdistnow sqrt (
; (([pxcor] of max-one-of patches in-radius 15 [totalattract] - [pxcor] of patch-here ) ^ 2) +
; ([pycor] of max-one-of patches in-radius 15 [totalattract] - [pycor] of patch-here ) ^ 2
; )
vs 2) use local variables and then calculate turtle movement
to move-turtles
let np patches in-radius 15 ; define your perceptual range
let bnp max-one-of np [totalattract] ; max of [totalattract] of patches in your neighborhood
let ah [totalattract] of patch-here ; [totalattract] of my patch
let xcorhere [pxcor] of patch-here
let ycorhere [pycor] of patch-here
let abnp [totalattract] of bnp
ifelse abnp - ah > 2 [
move-to bnp ; move if attractiveness of patches-here is lower then patches in-radius
let xbnp [pxcor] of bnp
let ybnp [pycor] of bnp
let flightdistnow sqrt ((xbnp - xcorhere) * (xbnp - xcorhere) + (ybnp - ycorhere) * (ybnp - ycorhere))
set t_dispers (t_dispers + flightdistnow)
set energy (energy - (flightdistnow / efficiency))
set flightdist (flightdist + flightdistnow)
; if ([pxcor] of patch-here = max-pxcor) or ([pycor] of patch-here = max-pycor) or ([pxcor] of patch-here = min-pxcor) or ([pycor] of patch-here = min-pycor)
; [set status "lost"
; set beetle_lost (beetle_lost + 1)]
] ; if attractivity of [totalattract] is higher the the one of my patch
and using stop watch for movement of 5000 turtles my results are:
- 1) 10 seconds
- 2) 5 seconds
so I suppose to use local variables in time consuming calculations.
I will appreciate if you will correct my conclusions if I'm wrong. Thanks !!
I am trying to use set to calculate a value, but it always return 0.5. When I breakdown the equation to individual operators, it works fine, but when I put it all together, all I get is 0.5. I think I have a syntax misunderstand. Any suggestions?
to go
;;see if a new risk is generated [randomly]
if random 2 = 1 ;;50% chance risk be generated on an individual project
[
make-risk
ask one-of projects
[
ask one-of risks [
set risk-encounter ((((RE-influence * ( [duration] of myself ) * 0.5)) / (( [duration] of myself )))) ;;calculate RE. THIS DOES NOT WORK :-( Always sets to 0.5
set temp-RE-score risk-encounter
]
set total-RE total-RE + temp-RE-score ;;update the project's RE score
]
]
ask projects[
set label precision total-RE 1
set size total-RE / 10 ; size according to RE score
]
tick
if ticks >= (max [duration] of projects ) * 2 [stop] ;; stops all the projects once max duration is reached (* 2)
end
Ive been looking around the net for ages tryin to find how to derive the 2d transformation matices for the above functions. Couldnt find it in my notes for college and it was a past exam question wondering if anybody could help for revision purposes? cheers
A transformation matrix is simply a short-hand for applying a function to the x and y values of a point, independently. In the case of translation, x' = 1*x + 0*y + dx*1 and y' = 0*x + 1*y + dy * 1. The matrix representation of these two equations is as follows:
[[ 1 0 dx ] [[ x ] [[ x' ]
[ 0 1 dy ] [ y ] = [ y' ]
[ 0 0 1 ]] [ 1 ]] [ 1 ]]
The other matrices can be similarly derived--simply determine what x' and y' should be, in terms of x, y and 1.
See Wikipedia, for instance.