Code Monkey home page Code Monkey logo

octave.net's Introduction

Octave.NET Build Status NuGet

Octave.NET

πŸ“ˆ More than cross-platform Octave process wrapper πŸ”¬

Read this, please!

If you encounter 'OctaveScriptError' it most likely means that your script is wrong due to some syntax error. Please, debug the generated script in Octave before creating issue in this repository. I can only assist you with problems related to this library but I can't provide Octave related support.

Motivation

Octave (matlab) has excellent library of ready to use components and lets us write simpler code for solving complex mathematical problems. This library is an attempt to bridge Octave and .NET worlds in a user-friendly and cross-platform manner while keeping single code-base and clean interface.

Installation

PM> Install-Package Octave.NET

Usage

  • Install latest octave
  • Add bin folder to system PATH variable or specify path to octave-cli binary in your code
  • Check the 'examples' folder

How It's Made?

This library spawns octave processes and controls them via standard streams (stdin, stdout and stderr). To keep optimal performance every time OctaveContext is disposed underlying octave-cli process is returned to the object pool, so we don't waste time on spawning new worker processes.

FAQ

How do I add octave-cli to system PATH variable?

I don't know, it varies between operating systems. Use your favourite search engine to find out.

Is there anything I should know before using it?

In single-threaded scenario, there will be only one worker process spawned. In multithreaded scenario, library will try to supply demand for OctaveContext's by spawning more processes until the limit is reached - by default the number of logical processors in your machine. If the limit is reached and all workers are in use, calling thread will be locked until some worker is freed. You may be interested in OctaveContext.OctaveSettings.MaximumConcurrency global setting.

What about applications that rarely use octave, are my resources wasted on idle processes?

Nope! After few seconds of inactivity (no attempts to execute octave commands) internal pool will start to slowly release its resources.

After a period of inactivity, first command execution takes a long time.

All processes were probably disposed and you experience "cold start". If you don't mind few more MBs that are not released until your application is closed, you can use global configuration and set OctaveContext.OctaveSettings.PreventColdStarts = true; Just remember to do this before first OctaveContext is created!

I set some variables in octave but later when I tried to access them they were undefined!

OctaveContext is called octave context because... well, it represent single octave context at the given point of time. Even though processes are reused, it is not guaranteed that you will get the same process. Your safest bet is to do all the work in single using(var octave = new OctaveContext()) {...} block. Contexts (e.g. octave variables) will not be cleared. If you require that, just executeclear command.

XYZ function does not work in octave - I get undefined!

If your script does not work when you run it manually in octave, it won't work there. Make sure that all packages that you need are installed and loaded.

I try to display plot but nothing appears.

There is an 2D plot example for that. "Async" octave operations are not supported, every operation should be locking and synchronous.

octave.net's People

Contributors

cptwesley avatar triforcely avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

octave.net's Issues

OctaveScriptError: error: sq_string cannot be indexed with

Hello I want Use it in My WebSite
so i print it to file
but it says 'OctaveScriptError: error: sq_string cannot be indexed with'
this is my code

var script = @"
x = -10:0.1:10;
y = sin (x);
handle = plot (x, y);
print('"+ uid + "'.png','-dpng') ";
 var a=   octave.Execute(script, int.MaxValue);

Library fails with "error: : all arguments must be strings" error message.

Hi, @CptWesley @triforcely

I met this error when run this code:

using (var octave = new OctaveContext())
{
// Note: your octave-cli must support some plot backend, in case of problems investigate manually in octave-cli

            var script = @"

x = -10:0.1:10;
y = sin (x);
filename= 'aa.txt';
save(filename, y);
handle = plot (x, y);

title (""Hello from C#"");
xlabel (""x"");
ylabel (""sin (x)"");

waitfor(handle); # <- without that plot window would not show
";
octave.Execute(script);
}

Error: "error: : all arguments must be strings\n"

What's wrong to me?

Thanks.

from @bemoregt.

Install problems

Hi, I am having issue installing from NuGet:

Severity Code Description Project File Line Suppression State
Error Could not install package 'Octave.NET 1.1.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6.1', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author. 0

I have met this error when run.

Hi, @CptWesley @triforcely

I have met this error when run.
"error: : all arguments must be strings\n"

What's wrong to me?

using System;
using System.IO;
using Octave.NET;

namespace ConsoleOctave1
{
class Program
{
static void Main(string[] args)
{
using (var octave = new OctaveContext())
{
octave.Execute("pkg load image;");
double[][] result = octave
.Execute("aa=peaks(256)")
.AsMatrix();// 10

            double[][] bb = octave
                .Execute("bb=aa.+1")
                .AsMatrix();// 10

            var cc = octave
                .Execute("f1='aa.mat'")
                .AsSpan();
                    
            var dd = octave
                .Execute("save(f1, bb);")    <------ Error occurs at here. ###########
                .AsMemory();// 10

            Console.WriteLine(bb[1][1].ToString());

            StreamWriter sw1;
            sw1 = new StreamWriter("data.txt");
            for (int i = 0; i < 256; i++)
            {
                for (int j = 0; j < 256; j++)
                {
                    sw1.WriteLine(bb[i][j].ToString());
                }
            }
            sw1.Dispose();

        }//using Octave
    }// method
}// class

}// namespace_

Thanks in advance.

How do I use variables?

My requirement is to input a double array and then do FFT using Octave, but I don’t know how to give the input to Octave.
by the way FFT output format is complex , can i use AsVector(); ??

public static void FFT_Octave(double[] signal)
{
double fre, amp;

using (var octave = new OctaveContext())
{
    var input = octave.Execute(signal.ToOctave()); // [??????]

    var vectorResult = octave
        .Execute("fft(input)")
        .AsVector();
}

}

I've successfully tested below code in OSX.

Hi, @CptWesley @triforcely

I've successfully tested below code in OSX.
Compiled in VS for mac.

using System;
using System.IO;
using Octave.NET;

namespace ConsoleOctave1
{
class Program
{
static void Main(string[] args)
{
using (var octave = new OctaveContext())
{
octave.Execute("pkg load image;");
double[][] result = octave
.Execute("aa=peaks(256)")
.AsMatrix();// 10

            Console.WriteLine(result[1][1].ToString());

            StreamWriter sw1;
            sw1 = new StreamWriter("data.txt");
            for (int i = 0; i < 256; i++)
            {
                for (int j = 0; j < 256; j++)
                {
                    sw1.WriteLine(result[i][j].ToString());
                }
            }
            sw1.Dispose();

        }//using Octave
    }// method
}// class

}// namespace

And Now, I want saving image from octave.
How can I save my octave image using Octave.net?

Unable to run 'octave-cli' executable. Adding octave-cli path to the code.

Adding the bin folder to system path was working fine with me, but I need to use it through code for deployment purposes, but it won't work correctly with me, it always through an exception that I've added details below.

My usage goes like that

OctaveContext.OctaveSettings.OctaveCliPath = @"D:\InstalledPrograms\Octave\Octave-5.1.0.0\mingw64\bin\"; // add octave logic using (var octave = new OctaveContext()) { octave.Execute("pkg load image;");

I've tried to change the path several changes by adding the octave-cli file name with and without the .exe extension. i've copied the file into my project path, but failed in all nonetheless.

How to specify path to octave-cli binary in my code? I'm using .Net framework 4.7 and 4.6.1 in my project.

Thank you so much.

Exception:
{ "Message": "An error has occurred.", "ExceptionMessage": "Unable to run 'octave-cli' executable. Make sure that it exists and/or is added to environment PATH variable.", "ExceptionType": "System.Exception", "StackTrace": " at Octave.NET.OctaveProcess..ctor(String octaveCliPath)\r\n .... "InnerException": { "Message": "An error has occurred.", "ExceptionMessage": "The system cannot find the file specified", "ExceptionType": "System.ComponentModel.Win32Exception", "StackTrace": " at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)\r\n at Octave.NET.OctaveProcess..ctor(String octaveCliPath)" }

Directly specify Octave executable

I was pleased to find this program, as it is exactly what I was searching for to solve a problem.

One issue I notice: the code works perfectly when I include Octave in my path. However, I do not think it is actually possible to specify the Octave executable through the code. This is because the OctaveContext creates the processPool item in its constructor, before anyone can have a chance to modify the OctaveSettings object. Perhaps it is possible to overload the constructor, and have an option to pass a customized OctaveSettings?

if script include a function, will throw out error

Hi,all:
I have defined a function in script, like:
disp("Running...") function result = myrand(n, t, p, d) a = 200 * t + p; big_rand = a * n; result = big_rand / 10**d; return;endfunction mrand = myrand(5379, 0, 91, 4)

the script can correct execute in octave, but when call:
octave.Execute(script)
will show error : "error: 'myrand' undefined near line 1, column 9\r\n"

how defined a function in script? thanks

The graphical interface cannot be displayed

I run the following commands on Octave-GUI, everything works fine and shows a 3D pattern interface. If I execute with Octave.NET, there is no graphical interface displayed.

[x, y, z] = sphere (40);
surf (3x, 3y, 3*z);
axis equal;
title ("sphere of radius 3");

What about 6.x compatibility ?

Hi there,

I was wondering what would be the compatibility with the latest Octave 6.x releases (6.2.0 at the time of writing)?
Are breaking changes usually expected with major releases (in essence what happened to make it compatible with 5.x two years ago)?

Regards

The graphic cannot be displayed

I run the following commands on Octave-GUI, everything works fine and shows a 3D pattern interface. If I execute with Octave.NET, there is no graphical interface displayed.

[x, y, z] = sphere (40);
surf (3x, 3y, 3*z);
axis equal;
title ("sphere of radius 3");

Octave 7.2.0

I have tested your solution with Octave 7.2.0 and you should know the application name 'octave-cli.exe' has changed to 'octave-launch.exe'. Also notice that 'octave-launch.exe' is now used for both GUI and CLI by using arguments:
GUI: "C:\Program Files\GNU Octave\Octave-7.2.0\octave-launch.exe" --gui
CLI: "C:\Program Files\GNU Octave\Octave-7.2.0\octave-launch.exe" --no-gui

  1. In OctaveGlobal.cs the application name is changed:

     private const string OctaveExecutable = "octave-launch";
    
  2. In OctaveProcess.cs the Arguments "--no-gui" is added:

         StartInfo = new ProcessStartInfo()
         {
             FileName = octaveCliPath,
             RedirectStandardInput = true,
             RedirectStandardOutput = true,
             RedirectStandardError = true,
             UseShellExecute = false,
             CreateNoWindow = true,
             Arguments = "--no-gui"
         };
    

Does it support Octave 4.4.1 on Windows 7/10?

I am having trouble running the example project.
I have Octave 4.4.1 installed as I can't find the links for 4.4.0 and have added "C:\Octave\Octave-4.4.1\bin" to the System Path variable.
The path never gets loaded in the octave object and it's causing to throw an exception.
If I manually specify the path to the bin, the result is the same.

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.