Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ait-rs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Janno
ait-rs
Commits
da600570
Commit
da600570
authored
8 years ago
by
Jan-Oliver Kaiser
Browse files
Options
Downloads
Patches
Plain Diff
Add memoization for log_fac_stirling.
parent
849ad30a
Branches
master
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/basic_codes/mod.rs
+33
-5
33 additions, 5 deletions
src/basic_codes/mod.rs
with
33 additions
and
5 deletions
src/basic_codes/mod.rs
+
33
−
5
View file @
da600570
...
...
@@ -10,9 +10,37 @@ pub mod stirling_approximation{
}
pub
fn
log_fac_stirling
(
n
:
f64
)
->
f64
{
let
result
:
f64
=
n
*
n
.ln
()
-
n
;
result
/
f64
::
consts
::
LN_2
use
std
;
use
atomic_f64
::
AtomicF64
;
const
MAX_LOG_FAC_MEMOIZATION
:
usize
=
1024
*
1024
;
lazy_static!
{
static
ref
LOG_FAC
:
Box
<
[
AtomicF64
;
MAX_LOG_FAC_MEMOIZATION
]
>
=
{
let
mut
result
:
Box
<
[
AtomicF64
;
MAX_LOG_FAC_MEMOIZATION
]
>
=
unsafe
{
box
std
::
mem
::
uninitialized
()
};
for
i
in
0
..
MAX_LOG_FAC_MEMOIZATION
{
let
uninit
=
std
::
mem
::
replace
(
&
mut
result
[
i
],
AtomicF64
::
new
(
0.0
));
std
::
mem
::
forget
(
uninit
)
};
result
};
}
#[inline]
pub
fn
log_fac_stirling
(
n
:
usize
)
->
f64
{
let
mut
result
:
f64
;
if
n
<
MAX_LOG_FAC_MEMOIZATION
{
let
result
=
LOG_FAC
[
n
-
1
]
.get
();
if
result
!=
0.0
{
return
result
}
}
result
=
((
n
*
n
)
as
f64
)
.ln
()
-
(
n
as
f64
);
result
/=
f64
::
consts
::
LN_2
;
if
n
<
MAX_LOG_FAC_MEMOIZATION
{
LOG_FAC
[
n
-
1
]
.set
(
result
)
}
result
}
...
...
@@ -25,8 +53,8 @@ pub mod stirling_approximation{
if
(
n
==
0
)
||
(
k
==
0
)
{
res
=
f64
::
INFINITY
;
}
else
{
let
n
=
n
as
f64
;
let
k
=
k
as
f64
;
//
let n = n as f64;
//
let k = k as f64;
res
=
log_fac_stirling
(
n
)
-
log_fac_stirling
(
k
)
-
log_fac_stirling
(
n
-
k
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment