Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add imf function #19

Merged
merged 4 commits into from
Jun 12, 2017
Merged

Add imf function #19

merged 4 commits into from
Jun 12, 2017

Conversation

TestSubjector
Copy link
Contributor

@TestSubjector TestSubjector commented Jun 12, 2017

I thought of making mass arguement as scalar and then writing another imf function that'll take the mass vector indices one by one (as in precess_xyz), but just restricting the mass argument to vector made things simpler.

* TODO.md: Remove 'imf' from list.
* docs/src/ref.md: Add "imf" entry to the manual.
* src/utils.jl: Add imf entry to "utils".
* src/imf.jl: Contains code of imf function.
* test/util-tests.jl: Include tests for imf.

Values now match (with the normal difference in least significant digits) with those
of IDL AstroLib.
@codecov-io
Copy link

codecov-io commented Jun 12, 2017

Codecov Report

Merging #19 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #19      +/-   ##
==========================================
+ Coverage   99.55%   99.56%   +<.01%     
==========================================
  Files          63       64       +1     
  Lines        1125     1144      +19     
==========================================
+ Hits         1120     1139      +19     
  Misses          5        5
Impacted Files Coverage Δ
src/imf.jl 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d4e578a...1b5c5fe. Read the comment docs.

Copy link
Member

@giordano giordano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree the input arrays are better in this case. I've only a few minor comments, but the patch looks otherwise good to me!

src/imf.jl Outdated
println("Length of array mass_range is not one more than that of expon")
return zeros(mass)
end
integ = ones(T, ne_comp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial value of integ doesn't look to be relevant. You can save some nanoseconds by initializing the vector with Vector{T}(ne_comp).

src/imf.jl Outdated
end
integ = ones(T, ne_comp)
joint = ones(T, ne_comp)
norm = ones(T, ne_comp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to initialize the vector here, this line can be removed.

src/imf.jl Outdated
joint[i] = joint[i-1]*(mass_range[i]^(expon[i-1] - expon[i]))
end
end
norm = (1/sum_kbn(integ.*joint))*joint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a particular reason why you used sum_kbn instead of the simpler sum? Did you find inaccurate results with it? Anyway, you can save an operation by using joint as numerator:

norm = joint ./ sum(integ .* joint)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought sum_kbn would give a significant boost in accuracy (as I was brought to believe from the docs). But apparently sum_kbn advantage was almost completely nerfed by JuliaLang/julia#4039. So yeah, using sum would be better.

Copy link
Member

@giordano giordano Jun 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sum_kbn should be indeed more accurate, for example in the case of elements spanning several orders of magnitude, but if this isn't the case we shouldn't worry too much ;-)

Anyway, I just realized that sum(integ .* joint) is simply the scalar product between the two vectors, so dot(integ, joint) is better. Sorry for not having noticed before.

src/imf.jl Outdated

### Example ###

```julia
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should probably go below ;-)

src/imf.jl Outdated
mass_range::AbstractVector{T})
ne_comp = length(expon)
if length(mass_range) != ne_comp + 1
println("Length of array mass_range is not one more than that of expon")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDL AstroLib throws an error here, instead of returning a vector of zeros. Why the different behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The option of using exception handling completely slipped my mind. Too much worrying about keeping return-type stable ;-)

Change sum() to dot() as only scalar product is involved
@giordano giordano merged commit cdb6081 into JuliaAstro:master Jun 12, 2017
@TestSubjector TestSubjector deleted the imf branch June 13, 2017 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants