I have latex string of a system equation:
\\left\\{ \\begin{array} { l } { y = - \\frac { 5} { x } } \\\\ { y = - 5} \\end{array} \\right.
but i don't know how to input or convert it into wolframalpha. Anyone know how to do it?
Your LaTeX string is incomplete or badly formatted. Alpha will interpret legitimate LaTeX.
Examples below in Mathematica and Alpha.
Related
I am writing an Google Scripts attached to Google Form and Google Sheet. I have a list of names where some have entered the data with lower case and upper case. I am trying to sort - however, the standard .sort() is sorting the Upper Case first and then the lower case - which is very confusing.
Could you suggest how i can sort a data so that it doesn't take into account the case for sorting - but retains the original uppwer and lower cases.
For e.g. var a = {Charlie, alpha, delta, Bravo};
Desired output {alpha, Bravo, Charlie, delta}.
Thank you.
Regards,
ray
You can define a custom function in javascript sort.
eg:
var a = ["Charlie", "alpha", "delta", "Bravo"];
a = a.sort(function(x, y){
x = x.toLowerCase()
y = y.toLowerCase()
if (x < y) {
return -1;
}
if (x > y) {
return 1;
}
return 0;
})
// Outputs [ "alpha", "Bravo", "Charlie", "delta" ]
I am unsure why I am getting an error.
I think it may stem from a misunderstanding around the structure syntax, but I am not certain if this is the issue (it would be unsurprising if there are multiple issues).
I am emulating code (from William Gould's The Mata Book) in which the input is a scalar, but the input for the program I am writing is a colvector.
The objective of this exercise is to create a square matrix from a column vector (according to some rules) and once created, multiply this square matrix by itself.
The code is the following:
*! spatial_lag version 1.0.0
version 15
set matastrict on
//--------------------------------------------------------------
local SL struct laginfo
local RS real scalar
local RC real colvector
local RM real matrix
//--------------------------------------------------------------
mata
`SL'
{
//-------------------inputs:
`RC' v
//-------------------derived:
`RM' W
`RM' W2
`RS' n
}
void lagset(`RC' v)
{
`SL' scalar r
// Input:
r.v = v
//I set the derived variables to missing:
r.W = .z
r.W2 = .z
r.n = .z // length of vector V
}
`RM' w_mat(`SL' scalar r)
{
if (r.W == .z) {
real scalar row, i
real scalar col, j
r.W = J(r.n,r.n,0)
for (i=1; i<=r.n; i++) {
for (i=1; i<=r.n; i++) {
if (j!=i) {
if (r.v[j]==r.v[i]) {
r.W[i,j] = 1
}
}
}
}
}
return(r.W)
}
`RS' wlength(`SL' scalar r)
{
if (r.n == .z) {
r.n = length(r.v)
}
return(r.n)
}
`RM' w2mat(`SL' scalar r)
{
if (r.W2 == .z) {
r.W2 = r.W * r.W
}
return(r.W2)
}
end
This compiles without a problem, but it give an error when I attempt to use it interactively as follows:
y=(1\1\1\2\2\2)
q = lagset(y)
w_mat(q)
w2mat(q)
The first two lines run fine, but when I run the last two of those lines, I get:
w_mat(): 3204 q[0,0] found where scalar required
<istmt>: - function returned error
What am I misunderstanding?
This particular error is unrelated to structures. Stata simply complains because the lagset() function is void. That is, it does not return anything. Thus, q ends up being empty, which is in turn used as input in the function w_mat() inappropriately - hence the q[0,0] reference.
The goal is simple - get the colour at (0, 0) and remove any pixels in the image that are similar to it within the specified threshold (16384 in this case). However, the code below doesn't compile:
#![feature(env, old_path, core, old_io)]
extern crate image;
use std::env;
use std::num::ToPrimitive;
use std::old_io::File;
use image::color::FromColor;
use image::Pixel;
fn get_arguments() -> Vec<String> {
let mut argv: Vec<String> = env::args().collect();
argv.remove(0);
return argv;
}
fn remove_background<T:image::GenericImage>(img: &mut T) {
let background_color = img.get_pixel(0, 0).to_rgba();
let transparent_pixel = image::Rgba([0, 0, 0, 0]);
if background_color[3].to_uint().unwrap() > 0 {
for (x, y, color) in img.pixels() {
let rgba = color.to_rgba();
let (dr,dg,db) = (rgba[0] - background_color[0],
rgba[1] - background_color[1],
rgba[2] - background_color[2]);
if (dr*dr + dg*dg + db*db).to_uint().unwrap() < 16384 { img.put_pixel(x, y, transparent_pixel); } // Remove the background colour.
}
}
}
fn main() {
for filepath in get_arguments() {
let img = image::open( &Path::new(filepath) ).unwrap();
remove_background( &mut img );
let ref mut fout = File::create(&Path::new("output.png")).unwrap();
img.save(fout, image::PNG);
}
}
It gives the following error:
src/main.rs:32:83: 32:100 error: mismatched types:
expected `<T as image::image::GenericImage>::Pixel`,
found `image::color::Rgba<_>`
(expected associated type,
found struct `image::color::Rgba`) [E0308]
src/main.rs:32 if (dr*dr + dg*dg + db*db).to_uint().unwrap() < 16384 { img.put_pixel(x, y, transparent_pixel); } // Remove the background colour.
This is presumably because the GenericImage struct defines its own internal "Pixel", which I don't think I can access, but is exactly the same as the normal Pixel struct. How would I get code with this functionality to compile? Every other use of put_pixel I've seen has used get_pixel on the image object and manipulated that, but I need to use a transparent pixel, so that won't work.
Chris Morgan is spot on - when you are accepting a GenericImage, you have to handle a generic Pixel. However, you are trying to use a specific one - Rgba. Even more than that, you have to specify the type of the channels of the Rgba.
A notable issue with your original code is: what do you do when the GenericImage is composed of pixels that don't support transparency?
Here's a version that picks some concrete types and compiles:
fn remove_background<T>(img: &mut T)
where T: image::GenericImage<Pixel=image::Rgba<u8>>
{
let background_color = img.get_pixel(0, 0).to_rgba();
if background_color[3].to_uint().unwrap() > 0 {
for (_, _, color) in img.pixels_mut() {
let rgba = color.to_rgba();
let (dr,dg,db) = (rgba[0] - background_color[0],
rgba[1] - background_color[1],
rgba[2] - background_color[2]);
// Remove the background colour.
if (dr*dr + dg*dg + db*db).to_uint().unwrap() < 16384 {
for c in color.channels_mut().iter_mut() { *c = 0 }
}
}
}
}
Beyond specifying the specific pixel type in the where clause, you'll also run into mutability issues. I changed it to pixels_mut, channels_mut, and iter_mut to get the mutability to the right place.
Note that the Rust style is 4-space indents, so I've done that as well.
I am trying to make a traffic light model in Alloy. The problem is that I don't really understand it well. I have been reading the traffic light example found with the analyzer, but for some reason it's not giving me any instance. This is the example code.
`module chapter4/lights ----- The model from page 127
abstract sig Color {}
one sig Red, Yellow, Green extends Color {}
sig Light {}
sig LightState {color: Light -> one Color}
sig Junction {lights: set Light}
fun redLights [s: LightState]: set Light { s.color.Red }
fun colorSequence: Color -> Color {
Color <: iden + Red->Green + Green->Yellow + Yellow->Red
}
pred mostlyRed [s: LightState, j: Junction] {
lone j.lights - redLights[s]
}
pred trans [s, s': LightState, j: Junction] {
lone x: j.lights | s.color[x] != s'.color[x]
all x: j.lights |
let step = s.color[x] -> s'.color[x] {
step in colorSequence
step in Red->(Color-Red) => j.lights in redLights[s]
}
}
assert Safe {
all s, s': LightState, j: Junction |
mostlyRed [s, j] and trans [s, s', j] => mostlyRed [s', j]
}
check Safe for 3 but 1 Junction`
If someone can please explain this, I would really appreciate it.
To see an instance, you need to include a run command. The only command you have here is a check command, and if the property checked is true, no counterexample will be found.
Simplified example of my slowly working code (the function rbf is from the kernlab package) that needs speeding up:
install.packages('kernlab')
library('kernlab')
rbf <- rbfdot(sigma=1)
test <- matrix(NaN,nrow=5,ncol=10)
for (i in 1:5) {
for (j in 1:10) { test[i,j] <- rbf(i,j)}
}
I've tried outer() but it doesn't work because the rbf function doesn't return the required length (50). I need to speed this code up because I have a huge amount of data. I've read that vectorization would be the holy grail to speeding this up but I don't know how.
Could you please point me in the right direction?
If rbf is indeed the return value from a call to rbfdot, then body(rbf) looks something like
{
if (!is(x, "vector"))
stop("x must be a vector")
if (!is(y, "vector") && !is.null(y))
stop("y must a vector")
if (is(x, "vector") && is.null(y)) {
return(1)
}
if (is(x, "vector") && is(y, "vector")) {
if (!length(x) == length(y))
stop("number of dimension must be the same on both data points")
return(exp(sigma * (2 * crossprod(x, y) - crossprod(x) -
crossprod(y))))
}
}
Since most of this is consists of check functions, and crossprod simplifies when you are only passing in scalars, I think your function simplifies to
rbf <- function(x, y, sigma = 1)
{
exp(- sigma * (x - y) ^ 2)
}
For a possible further speedup, use the compiler package (requires R-2.14.0 or later).
rbf_loop <- function(m, n)
{
out <- matrix(NaN, nrow = m, ncol = n)
for (i in seq_len(m))
{
for (j in seq_len(n))
{
out[i,j] <- rbf(i,j)
}
}
out
)
library(compiler)
rbf_loop_cmp <- cmpfun(rbf_loop)
Then compare the timing of rbf_loop_cmp(m, n) to what you had before.
The simplification step is easier to see in reverse. If you expand (x - y) ^ 2 you get x ^ 2 - 2 * x * y + y ^ 2, which is minus the thing in the rbf function.
Use the function kernelMatrix() in kernlab,
it should be a couple a couple of order of magnitudes
faster then looping over the kernel function:
library(kernlab)
rbf <- rbfdot(sigma=1)
kernelMatrix(rbf, 1:5, 1:10)