Search Unity

How to use Math.NET Numerics with Unity?

Discussion in 'Scripting' started by JoeStrout, Feb 18, 2014.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I need to do some matrix math, so after a bit of googling, I decided that the Math.NET Numerics library looks quite promising.

    So I downloaded the binaries package from here, unzipped it, and placed the MathNet.Numerics.dll file into the Assets folder of my project. But Unity immediately objected with:

    Code (csharp):
    1. Internal compiler error. See the console log for more information. output was:
    2. Unhandled Exception: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
    3.   at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)
    4.   at System.Reflection.Assembly.GetTypes () [0x00000] in <filename unknown>:0
    5.   at Mono.CSharp.RootNamespace.ComputeNamespaces (System.Reflection.Assembly assembly, System.Type extensionType) [0x00000] in <filename unknown>:0
    6.   at Mono.CSharp.RootNamespace.ComputeNamespace (Mono.CSharp.CompilerContext ctx, System.Type extensionType) [0x00000] in <filename unknown>:0
    7.   at Mono.CSharp.GlobalRootNamespace.ComputeNamespaces (Mono.CSharp.CompilerContext ctx) [0x00000] in <filename unknown>:0
    8.   at Mono.CSharp.Driver.LoadReferences () [0x00000] in <filename unknown>:0
    9.   at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0
    10.   at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0
    11.  
    I'm still pretty green at C#/Mono libraries and such, so hopefully I've just done something wrong... anybody have any idea why this doesn't work, and how to fix it?

    Or, is there another matrix math library you'd recommend?

    Many thanks,
    - Joe
     
  2. Gibbonator

    Gibbonator

    Joined:
    Jul 27, 2012
    Posts:
    204
    I haven't tried the dll with Unity but I have tried it with Mono by itself. I ran into problems with the dll requiring a later version of the CLR so unfortunately I didn't get it to work.
    Firstly you should make sure you're using the portable version of the dll rather than the .NET specific version. If that doesn't work then I would recommend using the older Iridium dll (http://iridium.mathdotnet.com/). That worked fine for me under Mono.

    Edit: Worth mentioning that it will not work in the free version of Unity since plugins are a Pro only feature.
     
    Last edited: Feb 18, 2014
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, you're talking about native code plugins; managed code DLLs work with any license.

    --Eric
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Looks like the latest versions require .NET 4 or higher which won't work with the Mono version that Unity rolls. I'd try getting an earlier version targeted at 3.5 and see if that works better.

    Alternately - grab the source and see if you can get it to build to 3.5. I ran into a weird issue with IronPython where the 3.5 binaries didn't work but compiling the source myself did.
     
  5. cdrnet

    cdrnet

    Joined:
    Mar 21, 2014
    Posts:
    1
    FYI, the latest Math.NET Numerics v3 pre-release packages from NuGet or Codeplex include proper .Net 3.5 builds (in addition to the PCL and .Net 4 builds)
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Thank you, that's good news!

    I've put the project where I needed some numerics on the back burner, because I got daunted by trying to build or find a suitable DLL. Perhaps I'll pull it out and give it another try.
     
  7. Marzoa

    Marzoa

    Joined:
    Dec 2, 2012
    Posts:
    50
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Nope, never got anywhere with it (mostly for lack of necessity, I think).
     
  9. eidercarlos

    eidercarlos

    Joined:
    May 14, 2014
    Posts:
    9
    Hello Guys!
    Have someone tried this?
    This worked to someone?
    Someone know other similar library?

    Please Help!!!
     
  10. Harinezumi

    Harinezumi

    Joined:
    Jan 7, 2013
    Posts:
    54
    I just did this, and it seems to work. As suggested above, I downloaded Math.Net Numerics from http://mathnetnumerics.codeplex.com/, and copied the .dlls MathNet.Numerics and System.Threading (the second is a dependency) into Assets/Plugins/ folder. Then in a script I just added using Math.Numerics;
     
    JoeStrout likes this.
  11. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    I'm sure the posters that asked a year and three months ago and two years ago respectively is very thankful that you repeated advice already given in the thread.

    ??????
     
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No need to be snarky. No one had previously confirmed that the codeplex DLLs actually work in Unity, and though my immediate need for it had evaporated, I can easily imagine needing it again someday. So, yes, I am thankful to @Harinezumi for confirmation that this works (and for pointing out the System.Threading dependency).
     
  13. TheD0ctor

    TheD0ctor

    Joined:
    Sep 29, 2016
    Posts:
    13
    [Warning shameless plug] I've submitted a port of the latest Math.NET Numerics for Unity (see here) which should be released soon, perhaps that could be of help.
     
  14. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No shame required, this is a useful service! Please let us know when it's available.
     
  15. TheD0ctor

    TheD0ctor

    Joined:
    Sep 29, 2016
    Posts:
    13
    Thanks! I will update this once it has been released to the Asset Store.
     
  16. TheD0ctor

    TheD0ctor

    Joined:
    Sep 29, 2016
    Posts:
    13
    Hi there, Math.NET for Unity has just been released (Math.NET for Unity Lite and Pro).
     
    JoeStrout likes this.
  17. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    I'm wondering, what do these offer aside from resolving dependency and version issue that may arise being Math.Net Numerics and Unity?

    Is there any extra features on top you've added?

    I'm only wondering because:

    1) you offer a Lite and Pro version, what's the difference?

    2) 65 & 95 dollars respectively??? Did you have to rewrite the whole library or something?
     
    Harinezumi likes this.
  18. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422

    Looks like pro includes the source, lite does not.
     
  19. TheD0ctor

    TheD0ctor

    Joined:
    Sep 29, 2016
    Posts:
    13
    Hi larku,
    Yes, Lite version does not include the source code. However, it has all the functionality of the Pro version and that is not going to change in future versions.
     
  20. TheD0ctor

    TheD0ctor

    Joined:
    Sep 29, 2016
    Posts:
    13
    At present Math.NET for Unity is a port of Math.NET Numerics so there are no extra features as such. That however is going to change. The direction of that change will depend on the users of the library and their needs.

    Maintaining a large and complex (Math-wise) library like Math.NET is far from trivial and hence time consuming. The price reflects that complexity and the required time commitment.
     
  21. Harinezumi

    Harinezumi

    Joined:
    Jan 7, 2013
    Posts:
    54
    I was wondering the same thing. Math.NET is already in C#, so you can easily use it as a plugin. In fact looking at the package content of "Math.NET for Unity", it does exactly that. So it is NOT a port (which is a good thing, ports are error prone). How and why would you port C# to C# anyway?

    Could the author please explain how is this asset different from the Math.NET library, and why is there a need for it? Otherwise I feel that it is ripping off people who don't know how to use plugins, and the only value it adds are the examples of how to use Math.NET.
     
    Last edited: Dec 20, 2016
  22. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Look, guys, if you don't feel it has value, then don't buy it.

    Some people's time is very valuable, and might prefer to click "Buy" in the Asset Store and have a nice neat Unity package known to work than to muck about finding, downloading, sorting through, and installing the correct package from some other site.

    If you feel strongly about it, nothing's stopping you from making your own Unity Math.NET asset and giving it away for free. But calling this one a rip-off is just being nasty, and our community is better than that.
     
    Munchy2007 and Dustin-Horne like this.
  23. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I get what you're saying but this isn't a port and the claim of additional maintenance of the code base seems suspect because the actual library hasn't been touched. I agree that the examples and documentation certainly took time but this whole thing smells of false advertising to me. I think folks here are just worried about users being misled.
     
    Harinezumi and lordofduct like this.
  24. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    I don't know if you'd say I feel "strongly" about it. Harinzumi might feel stronger than I do.

    I'm just wondering what they've added. For example:

    1) Have they done a barrage of testing within Unity to ensure it works? What tests are those that were done?
    2) If the pro version includes source, what's so special about this source vs the source on the Math.Net Numerics github?
    3) Did they have to do any extra porting that had to be done?
    4) What, if any, ADDED value is there in it aside from ironing out the kinks of getting it up and running in Unity?
    - for example there is a lot of GC heavy code within the Math.Net Numerics project which can hurt performance in Unity, has these things been approached?
    - another example, Math.Net probably won't work on mono subset 2.0, has changes been made to support that?

    100 bucks is a lot of money in my book, and I bet it's a lot of money to many other people here. And the package's description doesn't really outline what I'm getting over the FREE version.

    If it's just the headache of getting Numerics into the project... it's not really worth the 100 bucks to me. And I'm not going to KNOW if that's what it is without spending the 65 to 95 to find out.

    So, I guess TheD0ctor has giving a response to it though.

    All right, so it IS just getting Numerics into Unity.

    Now I can better judge for myself about purchasing it.

    And yes, they're correct in saying that there's a level of complexity to maintaining such a thing. Personally I don't find it that hard.

    Case in point... I pulled the latest off of git and there's a .Net 3.5 version of Numerics in that source. The team behind Math.Net Numerics is maintaining that. All I had to do is get it into Unity.

    So... I copied in the folder, added the compiler symbols NET35;NOSYSNUMERICS to the global defines (this is how the Math.Net team separates the 3.5 version from the full version), and all that was left was to copy the System.Threading.dll from the Math.Net project into my plugins folder; it's a limited 3.5 implementation of System.Threading.Tasks. Which actually makes me wonder if maybe TheD0ctor DID do extra work, because I'm not seeing this dll in their packages contents.

    Anyways, it appears to work, I ran a basic test of the threading stuff as that was my biggest concern and it worked.

    Took me all of 10 minutes, I've spent more time typing this response.

    Of course I haven't done a barrage of testing. But I don't know if TheD0ctor has done a barrage of testing either.

    And yeah, I don't get the examples that TheD0ctor has included... I don't need them either. I mean Math.Net Numerics has examples in their project anyways.

    And yeah, there is nothing stopping me...

    I've attached it to this post.

    And yes, it's actual source code, not a dll (aside from the System.Threading one).

    Of course it comes with 0 support on my part, and it's completely untested, and I won't be updating it with the latest version on the github. Of course, I'd probably also never use the unitypackage, and rather just pull from the original source as needed.

    But I've no idea what level of support or testing TheD0ctor has done either.

    And yeah, calling it a rip-off may be a bit rude (of course I wasn't the one who said it), but I could see concern for it being a potential rip-off... depending how one views the value of the work that TheD0ctor has done. So it's really just a matter of how much is 100 bucks worth to you in regards to the support you may get from TheD0ctor.

    No offense @TheD0ctor, just a member with some questions/concerns.
     

    Attached Files:

    Last edited: Dec 20, 2016
  25. TheD0ctor

    TheD0ctor

    Joined:
    Sep 29, 2016
    Posts:
    13
    I would like to thank you for your interest, questions and criticism.

    In answer to some comments we've received about Math.NET for Unity (e.g. by @KelsoMRK and @lordofduct) that, apparently, there seems to be no added value over the open source version of Math.NET Numerics. There is no universal answer to this as it depends on you as well.

    For example, if you're a game dev hobbyist, I would argue it would make little financial sense to buy Math.NET for Unity, as just for the fun of it you could spend some time and compile a version of Math.NET Numerics for your experiments (or use the package available above by @lordofduct). On the other hand, if you have a small to medium size professional game development studio and don't have the resources to hire a math postdoc but still want to have peace of mind knowing that your math library is working correctly and is fast, then, it would make sense to invest a small amount in an asset that satisfies that need.

    My original motivation for releasing this asset is that the linear algebra provider bundled with Math.NET Numerics, as usual with linear algebra libraries, is managed. That is to say, it uses a reference implementation of the BLAS and LAPACK libraries and not native optimised linear algebra providers. This makes the library, as is, quite slow. The good folks that created Math.NET Numerics gave people the option to actually connect their own linear algebra provider, assuming they have a license for MKL which is closed source and non-free (costs approximately $699) (see Math.NET Numerics Providers, this blog, and Intel MKL). Linear algebra is the basis for machine learning and quite a few AI algorithms are based on machine learning. In the current development branch of Math.NET for Unity this problem has been resolved for OSX and Windows. Support for other platforms will depend on the level of interest by the community. Given that, at the moment, the review cycle by the highly overworked reviewers at Unity is approximately 2.5 weeks, I intend to add an automatic verification using the Unity Asset Store API on our website to allow people who have already purchased any version of Math.NET for Unity to receive updates as soon as they become available.

    Another point that, in my view adds significant value, at least from a game dev company's perspective, is that someone qualified is looking after the port (a port doesn't have to be in another language) and making sure it is working correctly in the target platforms. I would argue my experience (research, degrees, work etc) is a good indication that I have had significant exposure with mathematics and similar libraries. That said, its healthy to be sceptical, so "google is your friend" in that regard (scroll down here for my name, and unappealing face, nothing I can do about that :)). This support is crucial for someone that is on a deadline. For example, the top voted requested feature in Math.NET Numerics was suggested in 2008, it started in 2013 and it still hasn't been implemented! This makes sense, the project is led by unpaid contributors.

    @KelsoMRK I think the description of Math.NET for Unity on the Asset Store is quite clear, however, to minimise the chance that people that don't really need the asset buy it by accident, we'll add a FAQ page on our website with more explanation and answers to comments and questions we've received.

    Lastly, Math.NET for Unity is not for everyone, and it wasn't intended to be for everyone. If the benefits of buying it from the asset store outweigh the costs for you, then by all means buy it. If on the other hand it doesn't make much financial sense for you to do so, that's okay as well. And if you have thoughtful comments, criticism or concerns like (@lordofduct - thanks, I know how long it takes to write a post like that), we'd love to hear them.
     
    JoeStrout likes this.
  26. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Thanks for the explanation - and to be clear I wasn't questioning the value of the plugin's existence, I was questioning what the plugin actually entailed.

    So just to be clear for folks who happen upon this thread later - have you modified the Math.NET Numerics library or did you simply get the DLL working and added examples? (You mentioned something about making the memory consumption better but then said that someone without a budget could just use what @lordofduct provided in the previous post - so I'm still confused.)
     
    jacobgmartin and Harinezumi like this.
  27. jacobgmartin

    jacobgmartin

    Joined:
    Sep 10, 2015
    Posts:
    37
    I want to thank everyone for this discussion!

    I too was wondering what the advantage is of TheD0ctor's Asset. The lack of his reply to KelsoMRK discouraged me. Like KelsoMRK, I'm still very confused why his asset costs so much money and even more confused as to what exactly he has done.

    So, I decided to try and import everything myself, using the excellent commentary by lordofduct. It was all rather easy: Import the src/Numerics source folder from the MathNet-Numerics github (https://github.com/mathnet/mathnet-numerics.git), import the System.Threading.dll into a directory named "Plugins/Resources" and then just make sure you go into the File->Build Settings->Player Settings->Scripting Define Symbols and add NET35;NOSYSNUMERICS.

    I then imported some relevant classes for testing.

    Code (CSharp):
    1. using MathNet.Numerics.LinearAlgebra;
    2. using MathNet.Numerics.LinearAlgebra.Single;
    3. using MathNet.Numerics.IntegralTransforms;
    To test it, I tried to make a matrix and print it out:

    Code (CSharp):
    1. // Test Mathnet.Numerics
    2. Matrix<float> testmatrix = DenseMatrix.OfArray(new float[,] {
    3.                                                                     {1,2,3,4},
    4.                                                                     {5,6,7,8},
    5.                                                                     {9,10,11,12}});
    6. Debug.Log("MATHNET.NUMERICS:   Matrix is " + testmatrix);
    It worked!!~

    I also queried the linear algebra provider:

    Code (CSharp):
    1. Debug.Log(MathNet.Numerics.Control.LinearAlgebraProvider.ToString());
    This printed "managed."

    So, I got really excited that everything "just worked!"

    However, when I attempted to do a IntegralTransforms.Fourier.ForwardReal() function call:

    Code (CSharp):
    1. double[] signal = new double[1024];
    2. Fourier.ForwardReal(signal,signal.Length, FourierOptions.NoScaling);
    I got the following error:

    Code (CSharp):
    1. MissingManifestResourceException: Could not find any resources appropriate for the specified culture or the neutral culture.  Make sure "MathNet.Numerics.Properties.Resources.resources" was correctly embedded or linked into assembly "Assembly-CSharp-firstpass" at compile time, or that all the satellite assemblies required are loadable and fully signed.
    2. System.Resources.ResourceManager.AssemblyResourceMissing (System.String fileName) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Resources/ResourceManager.cs:550)
    3. System.Resources.ResourceManager.InternalGetResourceSet (System.Globalization.CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Resources/ResourceManager.cs:394)
    4. System.Resources.ResourceManager.GetString (System.String name, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Resources/ResourceManager.cs:261)
    5. MathNet.Numerics.Properties.Resources.get_ArrayTooSmall () (at Assets/Plugins/Numerics/src/Properties/Resources.Designer.cs:565)
    6. MathNet.Numerics.IntegralTransforms.Fourier.ForwardReal (System.Double[] data, Int32 n, FourierOptions options) (at Assets/Plugins/Numerics/src/IntegralTransforms/Fourier.cs:127)
    Any ideas on how to fix this? I'm rather new to C# and don't know the terminology "culture" or "neutral culture." I googled around quite a bit but was at a loss for what to do.

    Many thanks in advance for any ideas, and for the excellent discussion!
     
    Last edited: Mar 12, 2017
    Harinezumi likes this.
  28. jacobgmartin

    jacobgmartin

    Joined:
    Sep 10, 2015
    Posts:
    37
    To answer my own question:

    The problem was that I was not calling the method ForwardReal properly. If a signal has an even length, N, then the data array must be N+2.

    So, the following code "almost works:"

    Code (CSharp):
    1. double[] m3 = new double[8192];
    2. Fourier.ForwardReal(m3,m3.Length-2);
    The issue still remains that the error messages are sort of hidden due to something with this "MissingManifestResourceException" which occurred above. I only figured it out when I saw the "MathNet.Numerics.Properties.Resources.get_ArrayTooSmall ()" section in the stack trace above.

    My issue now is that the above code (which I'm running on Gentoo Linux with Mono on Unity 5.5.2f) spits out an EntryPointNotFoundException:

    Code (CSharp):
    1. EntryPointNotFoundException: SwitchToThread
    2. System.Threading.Platform.Yield ()
    3. System.Threading.ManualResetEventSlim.Wait (Int32 millisecondsTimeout, CancellationToken cancellationToken)
    4. System.Threading.ManualResetEventSlim.Wait ()
    5. System.Threading.Tasks.Task.FastWaitAll (System.Threading.Tasks.Task[] tasks)
    6. System.Threading.Tasks.Parallel.Invoke (System.Threading.Tasks.ParallelOptions parallelOptions, System.Action[] actions)
    7. MathNet.Numerics.Threading.CommonParallel.Invoke (System.Action[] actions) (at Assets/Plugins/Numerics/src/Threading/CommonParallel.cs:153)
    8. MathNet.Numerics.IntegralTransforms.Fourier.BluesteinConvolutionParallel (MathNet.Numerics.Complex[] samples) (at Assets/Plugins/Numerics/src/IntegralTransforms/Fourier.Bluestein.cs:96)
    9. MathNet.Numerics.IntegralTransforms.Fourier.Bluestein (MathNet.Numerics.Complex[] samples, Int32 exponentSign) (at Assets/Plugins/Numerics/src/IntegralTransforms/Fourier.Bluestein.cs:168)
    10. MathNet.Numerics.IntegralTransforms.Fourier.BluesteinForward (MathNet.Numerics.Complex[] samples, FourierOptions options) (at Assets/Plugins/Numerics/src/IntegralTransforms/Fourier.cs:444)
    11. MathNet.Numerics.Providers.FourierTransform.ManagedFourierTransformProvider.Forward (MathNet.Numerics.Complex[] samples, FourierTransformScaling scaling) (at Assets/Plugins/Numerics/src/Providers/FourierTransform/ManagedFourierTransformProvider.cs:66)
    12. MathNet.Numerics.Providers.FourierTransform.ManagedFourierTransformProvider.ForwardReal (System.Double[] samples, Int32 n, FourierTransformScaling scaling) (at Assets/Plugins/Numerics/src/Providers/FourierTransform/ManagedFourierTransformProvider.cs:104)
    13. MathNet.Numerics.IntegralTransforms.Fourier.ForwardReal (System.Double[] data, Int32 n, FourierOptions options) (at Assets/Plugins/Numerics/src/IntegralTransforms/Fourier.cs:142)

    I'll post something further if I can figure it out.
     
  29. Harinezumi

    Harinezumi

    Joined:
    Jan 7, 2013
    Posts:
    54
    Hi jacobgmartin!

    Seems to me that this is a "linking" problem. Do you have System.Threading.dll and its xml in your project? Also, the xml of MathNet.Numerics is needed.
    (Maybe my suggestion is very basic, sorry if you already knew this!)
     
  30. jacobgmartin

    jacobgmartin

    Joined:
    Sep 10, 2015
    Posts:
    37
    Hello Harinezumi!!

    Thank you very much for the idea!

    I do have System.Threading.dll in my project, but I do not have an xml file for it. After looking around a lot, I don't even know from where I can get that file!

    I was building by the source of MathNet.Numerics, so I don't think I need its dll.... But, I guess I could try the dll version, and maybe that would help. Trouble is, I don't see a MathNet.Numerics.xml file either.

    Any ideas where I can find these xml files?

    Best regards!
     
  31. Harinezumi

    Harinezumi

    Joined:
    Jan 7, 2013
    Posts:
    54
    Hey!

    I used NuGet to install MathNet Numerics (https://www.nuget.org/packages/MathNet.Numerics), and that installed the xmls automatically. So that could be a possible way to get a built version (according to the official MS page NuGet works on Linux).
    However, now I'm not even sure the xmls are needed: according to this thread they only contain the comments and are supposedly used by IntelliSense.

    My next guess is that it could be a build issue, possibly a missing switch or wrong target platform. I hope this helps!

    Cheers!
     
  32. maxdefeber

    maxdefeber

    Joined:
    May 29, 2017
    Posts:
    4
    Dear @lordofduct and / or @jacobgmartin ,

    My wish is to also have my C# Unity script to work with the Math.NET Numerics framework. I've tried to do exactly as you guys did; but I can't get it to work. I'm not sure about three parts:

    1) You copy the "Numerics" folder from the GitHub Math.NET Numerics package, but where exactly do you paste it? I tried pasting it in the main folder of my Unity project. Maybe this is the wrong place?

    2) When you followed your steps (pasting the Numerics folder, adding NET35;NOSYSNUMERICS and pasting the .dll into the Plugins folder) --> Do you still have to install the MathNET Numerics package by running "Install-Package MathNet.Numerics" in the Package Manager Console of Visual Studio? Or can you now just use MathNet Numerics with "using MathNet.Numerics"?

    3) In the GitHub Math.NET Numerics package, I couldn't find the System Threading dll file you are using, so I took it from the unitypackage you made.

    I must have done something wrong in the above 3 steps; could you help me, explaining what I should do differently? This is a really specialistic question, for which Google search only yields this forum discussion.

    You'd be my heroes, I'd really appreciate your help!

    Best regards,
    Max de Feber
     
    luizatvieira likes this.
  33. Harinezumi

    Harinezumi

    Joined:
    Jan 7, 2013
    Posts:
    54
    Hey!

    I see that getting Math.NET Numerics to work is causing a lot of problems, but I use it every day, so I was wondering what's wrong. So I've just tried installing it into a completely empty project, but NuGet can't find any version that is compatible with Unity (5.6.0p4)! Moreover, I can't seem to get the Net35 version of the packages from the official site (although I did not try entering their OneDrive).

    So instead I just copied the DLLs from the project where it works into the Assets/Plugins/ folder of a completely new project and that did it. I'll just attach the DLLs, zipped (hopefully it doesn't violate any rules): https://www.dropbox.com/s/f6o6sgy95iwol3e/MathNet DLLs.zip?dl=0

    As said, just copy them into the Plugins folder, and in any source file you need Math.NET Numerics just add "using MathNet.Numerics;"

    Hope this helps!
     
    Last edited: Jun 28, 2017
  34. Harinezumi

    Harinezumi

    Joined:
    Jan 7, 2013
    Posts:
    54
    I couldn't let it rest, so I just tried the OneDrive, and from there you can get the .NET 3.5 compatible version of the latest MathNet.Numerics (the one I shared in zip is version 3.4, quite old)! I've just tried and it works! :)

    So to get it to work: go to MathNet.Numerics page (https://numerics.mathdotnet.com/), and on the right side select "NuGet & Binaries" (or directly: https://numerics.mathdotnet.com/Packages.html). Then click on Release archive link (first link in the text) and enter their OneDrive (you'll need a free Microsoft account, but you should have one if you use Visual Studio).

    Once in the MathNet.Numerics release archive enter the folder Zip, and just download the version of zipped release you want. You might want to scroll down and wait, OneDrive takes its time to load the file list (you can switch to list view on the top right corner).

    Once downloaded the zip, open it, enter the folder Net35, and copy MathNet.Numerics.dll and System.Threading.dll to your Assets/Plugins/ folder (or if you like to be organized Assets/Plugins/MathNet.Numerics/). And that's it! :)

    (You can also copy their xmls, but its not necessary.)
     
  35. maxdefeber

    maxdefeber

    Joined:
    May 29, 2017
    Posts:
    4
    Hi @Harinezumi,

    Many thanks for your explanation, which makes installling Math.NET Numerics on Unity very clear! I tried your solution, and it works.

    Now I would like to install the Math.NET Numerics Intel MKL (instructions on how to install it are in this link), which can speed up your matrix and vector math by a factor of 10x. I tried this on a regular C# project (without Unity), and the results are very promising. See the attached image.

    I have tried many things to install it, but I can't get it to work yet.

    Do you maybe know how to do this?
     

    Attached Files:

    Harinezumi likes this.
  36. maxdefeber

    maxdefeber

    Joined:
    May 29, 2017
    Posts:
    4
    Nevermind; this is fixed now! See: https://discuss.mathdotnet.com/t/net-3-5-nativemkl-provider/475/8
     
    Harinezumi likes this.
  37. Syntex91

    Syntex91

    Joined:
    Sep 13, 2017
    Posts:
    3
    Hi,

    following these instructions work if you build the unity project for Windows.

    I would like to get MathNet.Numerics working with Unity to build for UWP (Universal Windows Platform).
    Currently Unity won't build - plenty of errors.

    Did anyone build sucessfully for UWP?
     
    toncho11 likes this.
  38. DGJack

    DGJack

    Joined:
    Jul 25, 2014
    Posts:
    2
    Hm.. I trying to use UKF with this lib.

    I am facing this issue.

    E/Unity: MissingMethodException: Method not found: 'System.Security.PermissionSet.RevertAssert'.
    at System.Threading.ThreadLocal`1[MathNet.Numerics.Random.SystemRandomSource]..ctor (System.Func`1 valueFactory) [0x00000] in <filename unknown>:0
    at MathNet.Numerics.Random.SystemRandomSource..cctor () [0x00000] in <filename unknown>:0
    Rethrow as TypeInitializationException: An exception was thrown by the type initializer for MathNet.Numerics.Random.SystemRandomSource
    at MathNet.Numerics.LinearAlgebra.MatrixBuilder`1[System.Double].Random (Int32 rows, Int32 columns) [0x00000] in <filename unknown>:0
    at UnscentedKalmanFilter.UKF.init () [0x00000] in <filename unknown>:0
    at UnscentedKalmanFilter.UKF.Update (System.Double[] measurements) [0x00000] in <filename unknown>:0
     
  39. Harinezumi

    Harinezumi

    Joined:
    Jan 7, 2013
    Posts:
    54
    Based on the error, I think the System.Threading DLL is not linked correctly. Is this from a build, or is it in the Editor? What platform and architecture (32-bit vs. 64-bit) are you using, and is the DLL enabled for that platform and architecture?
     
  40. willszhuang

    willszhuang

    Joined:
    Apr 7, 2019
    Posts:
    2
    Hi Syntex,
    did you get it to work on UWP? It seems that mathnet.numerics is using some APIs that is not allowed in UWP/WSA.

    I get the same error as you. Did you use mathnet.numerics still or is there another library? i need to do fourier transformation with it..

    Thanks in advance.
     
  41. mprogers123

    mprogers123

    Joined:
    Feb 15, 2015
    Posts:
    10
    So I tried this -- and thank you for the suggestion. Unfortunately, now UnityEngine.UI is unrecognized (type or namespace does not exist). Any ideas why?
     
  42. billwaahau

    billwaahau

    Joined:
    Jan 14, 2017
    Posts:
    5
    Wow, maybe I am an idiot, but I think I finally figured this out! At least it didn't give me error right now.

    I tried the above methods with copying the DLLs over, but it didn't work with latest version of Unity 2019.3.0f3
    There is some issues with System.Threading.dll clashing with TextMesh Pro... something to do with Tasks.
    Probably because the version isn't compatible or something...

    Anyway, so basically, I follow the instruction on this site to use Nuget packages in Unity
    https://medium.com/@jonfoust/using-gcp-nuget-packages-with-unity-8dbd29c42cc4

    1) In Visual Studio, use Nuget Manager to install MathNet Numerics... if you go to Unity now, it will remove the reference and you will see an error message saying missing namespace if you try to use it.

    2) Go to root folder -> packages -> MathNet numerics -> lib -> netstandard2.0 (I am using .NET Standard 2.0)

    3) Copy the "netstandard2.0" folder inside an Asset folder (only that folder, else Unity will give an error about clashing framework)... the reason is that Unity can only detect stuff inside Asset.

    4) Make link.xml file ... the website said to do it so it will compile... idk, I did it just in case.

    Hopefully someone can do the community a favor by putting it in the Asset Store for Free! :)
     
  43. ArmanBM

    ArmanBM

    Joined:
    Dec 22, 2015
    Posts:
    1
  44. annabelfan

    annabelfan

    Joined:
    Dec 2, 2020
    Posts:
    1
    Just adding the MathNet.Numerics.dll (latest from MathNet.Numerics-4.9.1 > MathNet.Numerics > netstandard2.0) to a Plugins folder in Assets worked for me (at least visual studio is happy).

    But I am running into this error:
    NotSupportedException: Specified method is not supported.
    MathNet.Numerics.Providers.FourierTransform.Managed.ManagedFourierTransformProvider.ForwardMultidim (System.Numerics.Complex[] samples, System.Int32[] dimensions, MathNet.Numerics.Providers.FourierTransform.FourierTransformScaling scaling) (at <701ab8c93e9543a48624fbfba1017a49>:0)
    MathNet.Numerics.IntegralTransforms.Fourier.ForwardMultiDim (System.Numerics.Complex[] samples, System.Int32[] dimensions, MathNet.Numerics.IntegralTransforms.FourierOptions options) (at <701ab8c93e9543a48624fbfba1017a49>:0)
    MathNet.Numerics.IntegralTransforms.Fourier.Forward2D (System.Numerics.Complex[] samplesRowWise, System.Int32 rows, System.Int32 columns, MathNet.Numerics.IntegralTransforms.FourierOptions options) (at <701ab8c93e9543a48624fbfba1017a49>:0)

    when trying to call Fourier.Forward2D()

    Would anyone have an idea of what might be going wrong?
    (I've also tried changing the player settings to use .Net 4.x or IL2CPP but this doesn't seem to make a difference)